This repository was archived by the owner on Mar 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
feat: update samples #52
Merged
stephaniewang526
merged 15 commits into
googleapis:master
from
stephaniewang526:feat-samples
Dec 24, 2019
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6da1138
feat: add extract table to json sample
stephaniewang526 15f0945
feat: add create table sample
stephaniewang526 7032ee8
feat: add extract table to json sample
stephaniewang526 dcfc812
chore: clean up formatting
stephaniewang526 166ea16
chore: update formatting according to comment
stephaniewang526 116f215
chore: update formatting according to comment
stephaniewang526 d4a9223
update based on kurtis' comments
stephaniewang526 ea1c23b
Merge branch 'master' into feat-samples
stephaniewang526 1cce66f
updates based on comments
stephaniewang526 f6ba1c8
updates based on comments
stephaniewang526 0c00dcc
updates based on comments
stephaniewang526 672d2b1
updates based on comments
stephaniewang526 0ba4c34
remove unused storage deps
stephaniewang526 07fba2f
move function calls into try/catch block where appropriate
stephaniewang526 3441bbf
move BQ client construction into try/catch block
stephaniewang526 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java
This file was deleted.
Oops, something went wrong.
72 changes: 72 additions & 0 deletions
72
samples/src/main/java/com/example/bigquery/ExtractTableToJson.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| * Copyright 2019 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.bigquery; | ||
|
|
||
| // [START bigquery_extract_table] | ||
| import com.google.cloud.bigquery.BigQuery; | ||
| import com.google.cloud.bigquery.BigQueryException; | ||
| import com.google.cloud.bigquery.BigQueryOptions; | ||
| import com.google.cloud.bigquery.Job; | ||
| import com.google.cloud.bigquery.Table; | ||
| import com.google.cloud.bigquery.TableId; | ||
|
|
||
| public class ExtractTableToJson { | ||
|
|
||
| public static void runExtractTableToJson() { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String projectId = "bigquery-public-data"; | ||
| String datasetName = "samples"; | ||
| String tableName = "shakespeare"; | ||
| String bucketName = "my-bucket"; | ||
| String destinationUri = "gs://" + bucketName + "/path/to/file"; | ||
| extractTableToJson(projectId, datasetName, tableName, destinationUri); | ||
| } | ||
|
|
||
| // Exports datasetName:tableName to destinationUri as raw CSV | ||
| public static void extractTableToJson( | ||
| String projectId, String datasetName, String tableName, String destinationUri) { | ||
| try { | ||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. | ||
| BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); | ||
|
|
||
| TableId tableId = TableId.of(projectId, datasetName, tableName); | ||
| Table table = bigquery.getTable(tableId); | ||
|
|
||
| // For more information on export formats available see: | ||
| // https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types | ||
| // For more information on Job see: | ||
| // https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html | ||
| Job job = table.extract("CSV", destinationUri); | ||
|
|
||
| // Blocks until this job completes its execution, either failing or succeeding. | ||
| Job completedJob = job.waitFor(); | ||
|
stephaniewang526 marked this conversation as resolved.
|
||
| if (completedJob == null) { | ||
| System.out.println("Job not executed since it no longer exists."); | ||
| return; | ||
| } else if (completedJob.getStatus().getError() != null) { | ||
| System.out.println( | ||
| "BigQuery was unable to extract due to an error: \n" + job.getStatus().getError()); | ||
| return; | ||
| } | ||
| System.out.println("Table export successful. Check in GCS bucket for the CSV file."); | ||
| } catch (BigQueryException | InterruptedException e) { | ||
| System.out.println("Table extraction job was interrupted. \n" + e.toString()); | ||
| } | ||
| } | ||
| } | ||
| // [END bigquery_extract_table] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,28 +30,27 @@ public class UpdateDatasetAccess { | |
|
|
||
| public static void runUpdateDatasetAccess() { | ||
| // TODO(developer): Replace these variables before running the sample. | ||
| String datasetName = "my-dataset-name"; | ||
| String datasetName = "MY_DATASET_NAME"; | ||
| updateDatasetAccess(datasetName); | ||
| } | ||
|
|
||
| public static void updateDatasetAccess(String datasetName) { | ||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. | ||
| BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); | ||
| try { | ||
| // Initialize client that will be used to send requests. This client only needs to be created | ||
| // once, and can be reused for multiple requests. | ||
| BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); | ||
|
|
||
| Dataset dataset = bigquery.getDataset(datasetName); | ||
| Dataset dataset = bigquery.getDataset(datasetName); | ||
|
|
||
| // Create a new ACL granting the READER role to "[email protected]" | ||
| // For more information on the types of ACLs available see: | ||
| // https://cloud.google.com/storage/docs/access-control/lists | ||
| Acl newEntry = Acl.of(new User("[email protected]"), Role.READER); | ||
| // Create a new ACL granting the READER role to "[email protected]" | ||
| // For more information on the types of ACLs available see: | ||
| // https://cloud.google.com/storage/docs/access-control/lists | ||
| Acl newEntry = Acl.of(new User("[email protected]"), Role.READER); | ||
|
|
||
| // Get a copy of the ACLs list from the dataset and append the new entry | ||
| ArrayList<Acl> acls = new ArrayList<>(dataset.getAcl()); | ||
| acls.add(newEntry); | ||
| // Get a copy of the ACLs list from the dataset and append the new entry | ||
| ArrayList<Acl> acls = new ArrayList<>(dataset.getAcl()); | ||
| acls.add(newEntry); | ||
|
|
||
| // Update the dataset to use the new ACLs | ||
| try { | ||
| bigquery.update(dataset.toBuilder().setAcl(acls).build()); | ||
| System.out.println("Dataset Access Control updated successfully"); | ||
| } catch (BigQueryException e) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.