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 13 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
29 changes: 0 additions & 29 deletions samples/src/main/java/com/example/bigquery/ExtractTableToJSON.java

This file was deleted.

71 changes: 71 additions & 0 deletions samples/src/main/java/com/example/bigquery/ExtractTableToJson.java
@@ -0,0 +1,71 @@
/*
* 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) {
// 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();
stephaniewang526 marked this conversation as resolved.
Show resolved Hide resolved

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);
try {
// 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]
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
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

This file was deleted.

@@ -0,0 +1,71 @@
/*
* 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;
import static junit.framework.TestCase.assertNotNull;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class ExtractTableToJsonIT {
private ByteArrayOutputStream bout;
private PrintStream out;

private static final String GCS_BUCKET = System.getenv("GCS_BUCKET");

private static void requireEnvVar(String varName) {
assertNotNull(
"Environment variable '%s' is required to perform these tests.".format(varName),
System.getenv(varName));
}

@BeforeClass
public static void checkRequirements() {
requireEnvVar("GCS_BUCKET");
}

@Before
public void setUp() throws Exception {
bout = new ByteArrayOutputStream();
out = new PrintStream(bout);
System.setOut(out);
}

@After
public void tearDown() {
System.setOut(null);
}

@Test
public void testExtractTableToJson() {
String projectId = "bigquery-public-data";
String datasetName = "samples";
String tableName = "shakespeare";
String destinationUri = "gs://" + GCS_BUCKET + "/extractTest.csv";
System.out.println(destinationUri);

// Extract table content to GCS in CSV format
ExtractTableToJson.extractTableToJson(projectId, datasetName, tableName, destinationUri);
assertThat(bout.toString())
.contains("Table export successful. Check in GCS bucket for the CSV file.");
}
}