Skip to content

Commit ac9f589

Browse files
feat: update samples (#52)
* feat: add extract table to json sample * feat: add create table sample * feat: add extract table to json sample * chore: clean up formatting * chore: update formatting according to comment * chore: update formatting according to comment * update based on kurtis' comments * updates based on comments * updates based on comments * updates based on comments * updates based on comments * remove unused storage deps * move function calls into try/catch block where appropriate * move BQ client construction into try/catch block
1 parent 3f213c2 commit ac9f589

12 files changed

+241
-169
lines changed

samples/src/main/java/com/example/bigquery/CreateDataset.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ public class CreateDataset {
2727

2828
public static void runCreateDataset() {
2929
// TODO(developer): Replace these variables before running the sample.
30-
String datasetName = "my-dataset-name";
30+
String datasetName = "MY_DATASET_NAME";
3131
createDataset(datasetName);
3232
}
3333

3434
public static void createDataset(String datasetName) {
35-
// Initialize client that will be used to send requests. This client only needs to be created
36-
// once, and can be reused for multiple requests.
37-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38-
39-
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
4035
try {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests.
38+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
39+
40+
DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
41+
4142
Dataset newDataset = bigquery.create(datasetInfo);
4243
String newDatasetName = newDataset.getDatasetId().getDataset();
4344
System.out.println(newDatasetName + " created successfully");

samples/src/main/java/com/example/bigquery/CreateTable.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.example.bigquery;
218

319
// [START bigquery_create_table]
@@ -16,26 +32,26 @@ public class CreateTable {
1632

1733
public static void runCreateTable() {
1834
// TODO(developer): Replace these variables before running the sample.
19-
String datasetName = "my-dataset-name";
20-
String tableName = "my_table_name";
35+
String datasetName = "MY_DATASET_NAME";
36+
String tableName = "MY_TABLE_NAME";
2137
Schema schema =
2238
Schema.of(
23-
// LegacySQLTypeName will be updated to StandardSQLTypeName once release rolls out
39+
// INFO: LegacySQLTypeName will be updated to StandardSQLTypeName in release 1.103.0
2440
Field.of("stringField", LegacySQLTypeName.STRING),
2541
Field.of("booleanField", LegacySQLTypeName.BOOLEAN));
2642
createTable(datasetName, tableName, schema);
2743
}
2844

2945
public static void createTable(String datasetName, String tableName, Schema schema) {
30-
// Initialize client that will be used to send requests. This client only needs to be created
31-
// once, and can be reused for multiple requests.
32-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
46+
try {
47+
// Initialize client that will be used to send requests. This client only needs to be created
48+
// once, and can be reused for multiple requests.
49+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
3350

34-
TableId tableId = TableId.of(datasetName, tableName);
35-
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
36-
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
51+
TableId tableId = TableId.of(datasetName, tableName);
52+
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
53+
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
3754

38-
try {
3955
bigquery.create(tableInfo);
4056
System.out.println("Table created successfully");
4157
} catch (BigQueryException e) {

samples/src/main/java/com/example/bigquery/DeleteDataset.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,34 @@
1919
// [START bigquery_delete_dataset]
2020
import com.google.cloud.bigquery.BigQuery;
2121
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
22+
import com.google.cloud.bigquery.BigQueryException;
2223
import com.google.cloud.bigquery.BigQueryOptions;
2324
import com.google.cloud.bigquery.DatasetId;
2425

2526
public class DeleteDataset {
2627

2728
public static void runDeleteDataset() {
2829
// TODO(developer): Replace these variables before running the sample.\
29-
String projectId = "my-project-id";
30-
String datasetName = "my-dataset-name";
30+
String projectId = "MY_PROJECT_ID";
31+
String datasetName = "MY_DATASET_NAME";
3132
deleteDataset(projectId, datasetName);
3233
}
3334

3435
public static void deleteDataset(String projectId, String datasetName) {
35-
// Initialize client that will be used to send requests. This client only needs to be created
36-
// once, and can be reused for multiple requests.
37-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
36+
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
3840

39-
DatasetId datasetId = DatasetId.of(projectId, datasetName);
40-
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
41-
if (success) {
42-
System.out.println("Dataset deleted successfully");
43-
} else {
44-
System.out.println("Dataset was not found");
41+
DatasetId datasetId = DatasetId.of(projectId, datasetName);
42+
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
43+
if (success) {
44+
System.out.println("Dataset deleted successfully");
45+
} else {
46+
System.out.println("Dataset was not found");
47+
}
48+
} catch (BigQueryException e) {
49+
System.out.println("Dataset was not deleted. \n" + e.toString());
4550
}
4651
}
4752
}

samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.bigquery;
18+
19+
// [START bigquery_extract_table]
20+
import com.google.cloud.bigquery.BigQuery;
21+
import com.google.cloud.bigquery.BigQueryException;
22+
import com.google.cloud.bigquery.BigQueryOptions;
23+
import com.google.cloud.bigquery.Job;
24+
import com.google.cloud.bigquery.Table;
25+
import com.google.cloud.bigquery.TableId;
26+
27+
public class ExtractTableToJson {
28+
29+
public static void runExtractTableToJson() {
30+
// TODO(developer): Replace these variables before running the sample.
31+
String projectId = "bigquery-public-data";
32+
String datasetName = "samples";
33+
String tableName = "shakespeare";
34+
String bucketName = "my-bucket";
35+
String destinationUri = "gs://" + bucketName + "/path/to/file";
36+
extractTableToJson(projectId, datasetName, tableName, destinationUri);
37+
}
38+
39+
// Exports datasetName:tableName to destinationUri as raw CSV
40+
public static void extractTableToJson(
41+
String projectId, String datasetName, String tableName, String destinationUri) {
42+
try {
43+
// Initialize client that will be used to send requests. This client only needs to be created
44+
// once, and can be reused for multiple requests.
45+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
46+
47+
TableId tableId = TableId.of(projectId, datasetName, tableName);
48+
Table table = bigquery.getTable(tableId);
49+
50+
// For more information on export formats available see:
51+
// https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types
52+
// For more information on Job see:
53+
// https://googleapis.dev/java/google-cloud-clients/latest/index.html?com/google/cloud/bigquery/package-summary.html
54+
Job job = table.extract("CSV", destinationUri);
55+
56+
// Blocks until this job completes its execution, either failing or succeeding.
57+
Job completedJob = job.waitFor();
58+
if (completedJob == null) {
59+
System.out.println("Job not executed since it no longer exists.");
60+
return;
61+
} else if (completedJob.getStatus().getError() != null) {
62+
System.out.println(
63+
"BigQuery was unable to extract due to an error: \n" + job.getStatus().getError());
64+
return;
65+
}
66+
System.out.println("Table export successful. Check in GCS bucket for the CSV file.");
67+
} catch (BigQueryException | InterruptedException e) {
68+
System.out.println("Table extraction job was interrupted. \n" + e.toString());
69+
}
70+
}
71+
}
72+
// [END bigquery_extract_table]

samples/src/main/java/com/example/bigquery/ListDatasets.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,16 @@ public class ListDatasets {
2828

2929
public static void runListDatasets() {
3030
// TODO(developer): Replace these variables before running the sample.
31-
String projectId = "my-project-id";
31+
String projectId = "MY_PROJECT_ID";
3232
listDatasets(projectId);
3333
}
3434

3535
public static void listDatasets(String projectId) {
36-
// Initialize client that will be used to send requests. This client only needs to be created
37-
// once, and can be reused for multiple requests.
38-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
39-
40-
// List datasets in a specified project
4136
try {
37+
// Initialize client that will be used to send requests. This client only needs to be created
38+
// once, and can be reused for multiple requests.
39+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
40+
4241
Page<Dataset> datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100));
4342
for (Dataset dataset : datasets.iterateAll()) {
4443
System.out.println(dataset.getDatasetId() + " dataset in project listed successfully");

samples/src/main/java/com/example/bigquery/UpdateDatasetAccess.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,27 @@ public class UpdateDatasetAccess {
3030

3131
public static void runUpdateDatasetAccess() {
3232
// TODO(developer): Replace these variables before running the sample.
33-
String datasetName = "my-dataset-name";
33+
String datasetName = "MY_DATASET_NAME";
3434
updateDatasetAccess(datasetName);
3535
}
3636

3737
public static void updateDatasetAccess(String datasetName) {
38-
// Initialize client that will be used to send requests. This client only needs to be created
39-
// once, and can be reused for multiple requests.
40-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38+
try {
39+
// Initialize client that will be used to send requests. This client only needs to be created
40+
// once, and can be reused for multiple requests.
41+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
4142

42-
Dataset dataset = bigquery.getDataset(datasetName);
43+
Dataset dataset = bigquery.getDataset(datasetName);
4344

44-
// Create a new ACL granting the READER role to "[email protected]"
45-
// For more information on the types of ACLs available see:
46-
// https://cloud.google.com/storage/docs/access-control/lists
47-
Acl newEntry = Acl.of(new User("[email protected]"), Role.READER);
45+
// Create a new ACL granting the READER role to "[email protected]"
46+
// For more information on the types of ACLs available see:
47+
// https://cloud.google.com/storage/docs/access-control/lists
48+
Acl newEntry = Acl.of(new User("[email protected]"), Role.READER);
4849

49-
// Get a copy of the ACLs list from the dataset and append the new entry
50-
ArrayList<Acl> acls = new ArrayList<>(dataset.getAcl());
51-
acls.add(newEntry);
50+
// Get a copy of the ACLs list from the dataset and append the new entry
51+
ArrayList<Acl> acls = new ArrayList<>(dataset.getAcl());
52+
acls.add(newEntry);
5253

53-
// Update the dataset to use the new ACLs
54-
try {
5554
bigquery.update(dataset.toBuilder().setAcl(acls).build());
5655
System.out.println("Dataset Access Control updated successfully");
5756
} catch (BigQueryException e) {

samples/src/main/java/com/example/bigquery/UpdateDatasetDescription.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@ public class UpdateDatasetDescription {
2626

2727
public static void runUpdateDatasetDescription() {
2828
// TODO(developer): Replace these variables before running the sample.
29-
String datasetName = "my-dataset-name";
29+
String datasetName = "MY_DATASET_NAME";
3030
String newDescription = "this is the new dataset description";
3131
updateDatasetDescription(datasetName, newDescription);
3232
}
3333

3434
public static void updateDatasetDescription(String datasetName, String newDescription) {
35-
// Initialize client that will be used to send requests. This client only needs to be created
36-
// once, and can be reused for multiple requests.
37-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
38-
39-
Dataset dataset = bigquery.getDataset(datasetName);
40-
41-
// Update dataset description
4235
try {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests.
38+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
39+
40+
Dataset dataset = bigquery.getDataset(datasetName);
4341
bigquery.update(dataset.toBuilder().setDescription(newDescription).build());
4442
System.out.println("Dataset description updated successfully to " + newDescription);
4543
} catch (BigQueryException e) {

samples/src/main/java/com/example/bigquery/UpdateDatasetExpiration.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,20 @@ public class UpdateDatasetExpiration {
2727

2828
public static void runUpdateDatasetExpiration() {
2929
// TODO(developer): Replace these variables before running the sample.
30-
String datasetName = "my-dataset-name";
30+
String datasetName = "MY_DATASET_NAME";
3131
updateDatasetExpiration(datasetName);
3232
}
3333

3434
public static void updateDatasetExpiration(String datasetName) {
35-
// Initialize client that will be used to send requests. This client only needs to be created
36-
// once, and can be reused for multiple requests.
37-
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
35+
try {
36+
// Initialize client that will be used to send requests. This client only needs to be created
37+
// once, and can be reused for multiple requests.
38+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
3839

39-
Dataset dataset = bigquery.getDataset(datasetName);
40+
// Update dataset expiration to one day
41+
Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
4042

41-
// Update dataset expiration to one day
42-
Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
43-
try {
43+
Dataset dataset = bigquery.getDataset(datasetName);
4444
bigquery.update(dataset.toBuilder().setDefaultTableLifetime(newExpiration).build());
4545
System.out.println("Dataset description updated successfully to " + newExpiration);
4646
} catch (BigQueryException e) {

samples/src/test/java/com/example/bigquery/CreateTableIT.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package com.example.bigquery;
218

319
import static com.google.common.truth.Truth.assertThat;
@@ -36,7 +52,7 @@ public void testCreateTable() {
3652
CreateDataset.createDataset(generatedDatasetName);
3753

3854
// Create an empty table with specific schema in the dataset just created
39-
String tableName = "my_table_name";
55+
String tableName = "MY_TABLE_NAME";
4056
Schema schema =
4157
Schema.of(
4258
Field.of("stringField", LegacySQLTypeName.STRING),

0 commit comments

Comments
 (0)