Skip to content
Permalink
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
  • Loading branch information
stephaniewang526 committed Dec 24, 2019
1 parent 3f213c2 commit ac9f5899ae44e81887e8b4b0d3f6229e942f8d54
@@ -27,17 +27,18 @@ public class CreateDataset {

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

public static void createDataset(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();

DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();
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();

DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build();

Dataset newDataset = bigquery.create(datasetInfo);
String newDatasetName = newDataset.getDatasetId().getDataset();
System.out.println(newDatasetName + " created successfully");
@@ -1,3 +1,19 @@
/*
* 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_create_table]
@@ -16,26 +32,26 @@ public class CreateTable {

public static void runCreateTable() {
// TODO(developer): Replace these variables before running the sample.
String datasetName = "my-dataset-name";
String tableName = "my_table_name";
String datasetName = "MY_DATASET_NAME";
String tableName = "MY_TABLE_NAME";
Schema schema =
Schema.of(
// LegacySQLTypeName will be updated to StandardSQLTypeName once release rolls out
// INFO: LegacySQLTypeName will be updated to StandardSQLTypeName in release 1.103.0
Field.of("stringField", LegacySQLTypeName.STRING),
Field.of("booleanField", LegacySQLTypeName.BOOLEAN));
createTable(datasetName, tableName, schema);
}

public static void createTable(String datasetName, String tableName, Schema schema) {
// 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();

TableId tableId = TableId.of(datasetName, tableName);
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();
TableId tableId = TableId.of(datasetName, tableName);
TableDefinition tableDefinition = StandardTableDefinition.of(schema);
TableInfo tableInfo = TableInfo.newBuilder(tableId, tableDefinition).build();

try {
bigquery.create(tableInfo);
System.out.println("Table created successfully");
} catch (BigQueryException e) {
@@ -19,29 +19,34 @@
// [START bigquery_delete_dataset]
import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.DatasetId;

public class DeleteDataset {

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

public static void deleteDataset(String projectId, 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();

DatasetId datasetId = DatasetId.of(projectId, datasetName);
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
if (success) {
System.out.println("Dataset deleted successfully");
} else {
System.out.println("Dataset was not found");
DatasetId datasetId = DatasetId.of(projectId, datasetName);
boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents());
if (success) {
System.out.println("Dataset deleted successfully");
} else {
System.out.println("Dataset was not found");
}
} catch (BigQueryException e) {
System.out.println("Dataset was not deleted. \n" + e.toString());
}
}
}

This file was deleted.

@@ -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();
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]
@@ -28,17 +28,16 @@ public class ListDatasets {

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

public static void listDatasets(String projectId) {
// 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();

// List datasets in a specified project
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();

Page<Dataset> datasets = bigquery.listDatasets(projectId, DatasetListOption.pageSize(100));
for (Dataset dataset : datasets.iterateAll()) {
System.out.println(dataset.getDatasetId() + " dataset in project listed successfully");
@@ -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 "sample.bigquery.dev@gmail.com"
// 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("sample.bigquery.dev@gmail.com"), Role.READER);
// Create a new ACL granting the READER role to "sample.bigquery.dev@gmail.com"
// 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("sample.bigquery.dev@gmail.com"), 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) {
@@ -26,20 +26,18 @@ public class UpdateDatasetDescription {

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

public static void updateDatasetDescription(String datasetName, String newDescription) {
// 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);

// Update dataset description
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);
bigquery.update(dataset.toBuilder().setDescription(newDescription).build());
System.out.println("Dataset description updated successfully to " + newDescription);
} catch (BigQueryException e) {
@@ -27,20 +27,20 @@ public class UpdateDatasetExpiration {

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

public static void updateDatasetExpiration(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);
// Update dataset expiration to one day
Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);

// Update dataset expiration to one day
Long newExpiration = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
try {
Dataset dataset = bigquery.getDataset(datasetName);
bigquery.update(dataset.toBuilder().setDefaultTableLifetime(newExpiration).build());
System.out.println("Dataset description updated successfully to " + newExpiration);
} catch (BigQueryException e) {
@@ -1,3 +1,19 @@
/*
* 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;

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

// Create an empty table with specific schema in the dataset just created
String tableName = "my_table_name";
String tableName = "MY_TABLE_NAME";
Schema schema =
Schema.of(
Field.of("stringField", LegacySQLTypeName.STRING),

0 comments on commit ac9f589

Please sign in to comment.