From 4455cb7288025dcbe108253a3f48a4e73abaa72e Mon Sep 17 00:00:00 2001 From: Kurtis Van Gent <31518063+kurtisvg@users.noreply.github.com> Date: Mon, 22 Jan 2018 10:06:56 -0800 Subject: [PATCH] Update BigQuery Samples. --- .../bigquery/QueryParametersSample.java | 66 ++----------------- .../com/example/bigquery/QuerySample.java | 6 +- .../java/com/example/bigquery/SimpleApp.java | 4 +- 3 files changed, 11 insertions(+), 65 deletions(-) diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/QueryParametersSample.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/QueryParametersSample.java index 771ed9c9a71..9690b51ade4 100644 --- a/bigquery/cloud-client/src/main/java/com/example/bigquery/QueryParametersSample.java +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/QueryParametersSample.java @@ -21,8 +21,7 @@ import com.google.cloud.bigquery.FieldValue; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryParameterValue; -import com.google.cloud.bigquery.QueryResponse; -import com.google.cloud.bigquery.QueryResult; +import com.google.cloud.bigquery.TableResult; import java.io.IOException; import java.util.Arrays; import java.util.List; @@ -112,8 +111,7 @@ public static void main(final String[] args) throws IOException, InterruptedExce // [START bigquery_query_params] private static void runNamed(final String corpus, final long minWordCount) throws InterruptedException { - BigQuery bigquery = - new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance()); + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); String queryString = "SELECT word, word_count\n" @@ -131,25 +129,9 @@ private static void runNamed(final String corpus, final long minWordCount) .build(); // Execute the query. - QueryResponse response = bigquery.query(queryRequest); - - // Wait for the job to finish (if the query takes more than 10 seconds to complete). - while (!response.jobCompleted()) { - Thread.sleep(1000); - response = bigquery.getQueryResults(response.getJobId()); - } - - // Check for errors. - if (response.hasErrors()) { - String firstError = ""; - if (response.getExecutionErrors().size() != 0) { - firstError = response.getExecutionErrors().get(0).getMessage(); - } - throw new RuntimeException(firstError); - } + TableResult result = bigquery.query(queryRequest); // Print all pages of the results. - QueryResult result = response.getResult(); while (result != null) { for (List row : result.iterateAll()) { System.out.printf("%s: %d\n", row.get(0).getStringValue(), row.get(1).getLongValue()); @@ -165,8 +147,7 @@ private static void runNamed(final String corpus, final long minWordCount) */ // [START bigquery_query_params_arrays] private static void runArray(String gender, String[] states) throws InterruptedException { - BigQuery bigquery = - new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance()); + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); String queryString = "SELECT name, sum(number) as count\n" @@ -186,25 +167,9 @@ private static void runArray(String gender, String[] states) throws InterruptedE .build(); // Execute the query. - QueryResponse response = bigquery.query(queryRequest); - - // Wait for the job to finish (if the query takes more than 10 seconds to complete). - while (!response.jobCompleted()) { - Thread.sleep(1000); - response = bigquery.getQueryResults(response.getJobId()); - } - - // Check for errors. - if (response.hasErrors()) { - String firstError = ""; - if (response.getExecutionErrors().size() != 0) { - firstError = response.getExecutionErrors().get(0).getMessage(); - } - throw new RuntimeException(firstError); - } + TableResult result = bigquery.query(queryRequest); // Print all pages of the results. - QueryResult result = response.getResult(); while (result != null) { for (List row : result.iterateAll()) { System.out.printf("%s: %d\n", row.get(0).getStringValue(), row.get(1).getLongValue()); @@ -217,8 +182,7 @@ private static void runArray(String gender, String[] states) throws InterruptedE // [START bigquery_query_params_timestamps] private static void runTimestamp() throws InterruptedException { - BigQuery bigquery = - new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance()); + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); DateTime timestamp = new DateTime(2016, 12, 7, 8, 0, 0, DateTimeZone.UTC); @@ -236,25 +200,9 @@ private static void runTimestamp() throws InterruptedException { .build(); // Execute the query. - QueryResponse response = bigquery.query(queryRequest); - - // Wait for the job to finish (if the query takes more than 10 seconds to complete). - while (!response.jobCompleted()) { - Thread.sleep(1000); - response = bigquery.getQueryResults(response.getJobId()); - } - - // Check for errors. - if (response.hasErrors()) { - String firstError = ""; - if (response.getExecutionErrors().size() != 0) { - firstError = response.getExecutionErrors().get(0).getMessage(); - } - throw new RuntimeException(firstError); - } + TableResult result = bigquery.query(queryRequest); // Print all pages of the results. - QueryResult result = response.getResult(); DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC(); while (result != null) { for (List row : result.iterateAll()) { diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/QuerySample.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/QuerySample.java index ef00c6cc698..26e182fc8b4 100644 --- a/bigquery/cloud-client/src/main/java/com/example/bigquery/QuerySample.java +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/QuerySample.java @@ -23,9 +23,8 @@ import com.google.cloud.bigquery.JobId; import com.google.cloud.bigquery.JobInfo; import com.google.cloud.bigquery.QueryJobConfiguration; -import com.google.cloud.bigquery.QueryResponse; -import com.google.cloud.bigquery.QueryResult; import com.google.cloud.bigquery.TableId; +import com.google.cloud.bigquery.TableResult; import java.io.IOException; import java.util.List; import java.util.UUID; @@ -137,8 +136,7 @@ public static void runQuery(QueryJobConfiguration queryConfig) } // Get the results. - QueryResponse response = bigquery.getQueryResults(jobId); - QueryResult result = response.getResult(); + TableResult result = queryJob.getQueryResults(); // Print all pages of the results. while (result != null) { diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java index 4b5179ceb5b..37a278984ad 100644 --- a/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/SimpleApp.java @@ -27,7 +27,7 @@ import com.google.cloud.bigquery.JobInfo; import com.google.cloud.bigquery.QueryJobConfiguration; import com.google.cloud.bigquery.QueryResponse; -import com.google.cloud.bigquery.QueryResult; +import com.google.cloud.bigquery.TableResult; import java.util.UUID; // [END bigquery_simple_app_deps] @@ -71,7 +71,7 @@ public static void main(String... args) throws Exception { // Get the results. QueryResponse response = bigquery.getQueryResults(jobId); - QueryResult result = response.getResult(); + TableResult result = queryJob.getQueryResults(); // Print all pages of the results. for (FieldValueList row : result.iterateAll()) {