Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update samples #52

Merged
merged 15 commits into from Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions samples/src/main/java/com/example/bigquery/CreateDataset.java
Expand Up @@ -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");
Expand Down
36 changes: 26 additions & 10 deletions samples/src/main/java/com/example/bigquery/CreateTable.java
@@ -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]
Expand All @@ -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 =
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
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) {
Expand Down
27 changes: 16 additions & 11 deletions samples/src/main/java/com/example/bigquery/DeleteDataset.java
Expand Up @@ -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());
}
}
}
Expand Down
29 changes: 0 additions & 29 deletions samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java

This file was deleted.

72 changes: 72 additions & 0 deletions samples/src/main/java/com/example/bigquery/ExtractTableToJson.java
@@ -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.
Show resolved Hide 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]
11 changes: 5 additions & 6 deletions samples/src/main/java/com/example/bigquery/ListDatasets.java
Expand Up @@ -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");
Expand Down
27 changes: 13 additions & 14 deletions samples/src/main/java/com/example/bigquery/UpdateDatasetAccess.java
Expand Up @@ -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) {
Expand Down
Expand Up @@ -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) {
Expand Down
Expand Up @@ -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) {
Expand Down
18 changes: 17 additions & 1 deletion samples/src/test/java/com/example/bigquery/CreateTableIT.java
@@ -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;
Expand Down Expand Up @@ -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),
Expand Down