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 10 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
Expand Up @@ -27,7 +27,7 @@ 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);
}

Expand Down
22 changes: 19 additions & 3 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,11 +32,11 @@ 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);
Expand Down
4 changes: 2 additions & 2 deletions samples/src/main/java/com/example/bigquery/DeleteDataset.java
Expand Up @@ -26,8 +26,8 @@ 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);
}

Expand Down
49 changes: 42 additions & 7 deletions samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java
@@ -1,26 +1,61 @@
/*
* 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.
Table table = null;
String format = "CSV";
String projectId = "bigquery-public-data";
String datasetName = "samples";
String tableName = "shakespeare";
String bucketName = "my-bucket";
String gcsFileName = "gs://" + bucketName + "/extractTest.csv";
extractTableToJSON(table, format, gcsFileName);
String destinationUri = "gs://" + bucketName + "/path/to/file";
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved

// Extract table
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
extractTableToJSON(projectId, datasetName, tableName, destinationUri);
}

// Exports my-dataset-name:my_table to gcs://my-bucket/my-file as raw CSV
public static void extractTableToJSON(Table table, String format, String gcsFileName) {
public static void extractTableToJSON(
String projectId, String datasetName, String tableName, String destinationUri) {
// 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 format available see:
// https://cloud.google.com/bigquery/docs/exporting-data#export_formats_and_compression_types
Job job = table.extract("CSV", destinationUri);
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
try {
table.extract(format, gcsFileName);
System.out.println("Table extraction job completed successfully");
if (job != null && job.getStatus().getError() == null)
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
System.out.println(
"Table extraction job completed successfully. Check in GCS bucket for the CSV file.");
else System.out.println("Table extraction job failed");
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
} catch (BigQueryException e) {
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
System.out.println("Table extraction job was interrupted. \n" + e.toString());
}
Expand Down
Expand Up @@ -28,7 +28,7 @@ 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);
}

Expand Down
Expand Up @@ -30,7 +30,7 @@ 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);
}

Expand Down
Expand Up @@ -26,7 +26,7 @@ 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);
}
Expand Down
Expand Up @@ -27,7 +27,7 @@ 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);
}

Expand Down
Expand Up @@ -36,7 +36,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
Expand Up @@ -2,18 +2,6 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryException;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.Field;
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;
import com.google.cloud.bigquery.StandardTableDefinition;
import com.google.cloud.bigquery.Table;
import com.google.cloud.bigquery.TableDefinition;
import com.google.cloud.bigquery.TableId;
import com.google.cloud.bigquery.TableInfo;
import com.google.cloud.bigquery.testing.RemoteBigQueryHelper;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
Expand All @@ -38,39 +26,9 @@ public void tearDown() {

@Test
public void testExtractTableToJSON() {
String generatedDatasetName = RemoteBigQueryHelper.generateDatasetName();

// Create a new dataset to create a new table in
CreateDataset.createDataset(generatedDatasetName);

// Create a new table to extract to GCS for
String tableName = "my_table_name";
Schema schema =
Schema.of(
Field.of("stringField", LegacySQLTypeName.STRING),
Field.of("booleanField", LegacySQLTypeName.BOOLEAN));
Table table = createTableHelper(generatedDatasetName, tableName, schema);

// Extract table content to GCS in CSV format
ExtractTableToJSON.extractTableToJSON(table, "CSV", "gs://my-bucket/extractTest.csv");
ExtractTableToJSON.extractTableToJSON(
"bigquery-public-data", "samples", "shakespeare", "gs://my-bucket/extractTest.csv");
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved
assertThat(bout.toString()).contains("Table extraction job completed successfully");
}

private static Table createTableHelper(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();

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

try {
Table table = bigquery.create(tableInfo);
return table;
} catch (BigQueryException e) {
System.out.println("Table was not created. \n" + e.toString());
return null;
}
}
}