From 2aeedac9c2d38ad792d52eb93742171963ab9998 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Fri, 16 Sep 2016 10:54:47 -0700 Subject: [PATCH] Add BigQuery snippets for Job.isDone(). Runs google-java-format against the BigQuery snippets, too. --- .../snippets/InsertDataAndQueryTable.java | 15 +++--- .../bigquery/snippets/JobSnippets.java | 51 ++++++++++++++++++ .../bigquery/snippets/ITJobSnippets.java | 53 +++++++++++++++++++ 3 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java create mode 100644 google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/InsertDataAndQueryTable.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/InsertDataAndQueryTable.java index 6fb2a7dce997..6022545eb3bf 100644 --- a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/InsertDataAndQueryTable.java +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/InsertDataAndQueryTable.java @@ -70,10 +70,8 @@ public static void main(String... args) throws InterruptedException { firstRow.put("StringField", "value1"); secondRow.put("StringField", "value2"); // Create an insert request - InsertAllRequest insertRequest = InsertAllRequest.builder(tableId) - .addRow(firstRow) - .addRow(secondRow) - .build(); + InsertAllRequest insertRequest = + InsertAllRequest.builder(tableId).addRow(firstRow).addRow(secondRow).build(); // Insert rows InsertAllResponse insertResponse = bigquery.insertAll(insertRequest); // Check if errors occurred @@ -82,10 +80,11 @@ public static void main(String... args) throws InterruptedException { } // Create a query request - QueryRequest queryRequest = QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id") - .maxWaitTime(60000L) - .pageSize(1000L) - .build(); + QueryRequest queryRequest = + QueryRequest.builder("SELECT * FROM my_dataset_id.my_table_id") + .maxWaitTime(60000L) + .pageSize(1000L) + .build(); // Request query to be executed and wait for results QueryResponse queryResponse = bigquery.query(queryRequest); while (!queryResponse.jobCompleted()) { diff --git a/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java new file mode 100644 index 000000000000..5552b5ff9968 --- /dev/null +++ b/google-cloud-examples/src/main/java/com/google/cloud/examples/bigquery/snippets/JobSnippets.java @@ -0,0 +1,51 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * 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. + */ + +/* + * EDITING INSTRUCTIONS + * This file is referenced in Job's javadoc. Any change to this file should be reflected in + * Job's javadoc. + */ + +package com.google.cloud.examples.bigquery.snippets; + +import com.google.cloud.bigquery.Job; + +public class JobSnippets { + + private final Job job; + + public JobSnippets(Job job) { + this.job = job; + } + + /** + * Example of waiting for a job until it reports that it is done. + */ + // [TARGET isDone()] + public boolean isDone() { + try { + // [START isDone] + while (!job.isDone()) { + Thread.sleep(1000L); + } + // [END isDone] + } catch (InterruptedException e) { + return false; + } + return true; + } +} diff --git a/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java new file mode 100644 index 000000000000..eb3f49f0dd6e --- /dev/null +++ b/google-cloud-examples/src/test/java/com/google/cloud/examples/bigquery/snippets/ITJobSnippets.java @@ -0,0 +1,53 @@ +/* + * Copyright 2016 Google Inc. All Rights Reserved. + * + * 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.google.cloud.examples.bigquery.snippets; + +import static org.junit.Assert.assertTrue; + +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.Job; +import com.google.cloud.bigquery.JobConfiguration; +import com.google.cloud.bigquery.JobInfo; +import com.google.cloud.bigquery.QueryJobConfiguration; +import com.google.cloud.bigquery.testing.RemoteBigQueryHelper; + +import org.junit.BeforeClass; +import org.junit.Test; + +public class ITJobSnippets { + + private static BigQuery bigquery; + + @BeforeClass + public static void beforeClass() { + bigquery = RemoteBigQueryHelper.create().options().service(); + } + + @Test + public void testIsDone() throws Exception { + JobConfiguration jobConfig = + QueryJobConfiguration.builder( + "SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;") + .useLegacySql(false) + .build(); + JobInfo jobInfo = JobInfo.builder(jobConfig).build(); + Job job = bigquery.create(jobInfo); + JobSnippets jobSnippets = new JobSnippets(job); + boolean result = jobSnippets.isDone(); + assertTrue(result); + } +}