From 7fdfb8b82c4f063ca359194d605626a89d980332 Mon Sep 17 00:00:00 2001 From: Tim Swast Date: Tue, 28 Mar 2017 15:17:16 -0700 Subject: [PATCH] Use Java 7 for BQ samples. --- bigquery/cloud-client/pom.xml | 4 +-- .../bigquery/QueryParametersSample.java | 34 ++++++++----------- .../com/example/bigquery/SyncQuerySample.java | 27 +++++++++------ .../example/bigquery/SyncQuerySampleIT.java | 5 ++- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/bigquery/cloud-client/pom.xml b/bigquery/cloud-client/pom.xml index 89fd11d9a41..f357faf0f7f 100644 --- a/bigquery/cloud-client/pom.xml +++ b/bigquery/cloud-client/pom.xml @@ -28,8 +28,8 @@ - 1.8 - 1.8 + 1.7 + 1.7 UTF-8 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 2485fc0cf83..97f08a563c2 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 @@ -32,7 +32,6 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; -import java.util.stream.Collectors; /** * A sample that demonstrates use of query parameters. @@ -150,12 +149,11 @@ private static void runNamed(final String corpus, final long minWordCount) } if (response.hasErrors()) { - throw new RuntimeException( - response - .getExecutionErrors() - .stream() - .map(err -> err.getMessage()) - .collect(Collectors.joining("\n"))); + String firstError = ""; + if (response.getExecutionErrors().size() != 0) { + firstError = response.getExecutionErrors().get(0).getMessage(); + } + throw new RuntimeException(firstError); } QueryResult result = response.getResult(); @@ -208,12 +206,11 @@ private static void runArray(String gender, String[] states) } if (response.hasErrors()) { - throw new RuntimeException( - response - .getExecutionErrors() - .stream() - .map(err -> err.getMessage()) - .collect(Collectors.joining("\n"))); + String firstError = ""; + if (response.getExecutionErrors().size() != 0) { + firstError = response.getExecutionErrors().get(0).getMessage(); + } + throw new RuntimeException(firstError); } QueryResult result = response.getResult(); @@ -256,12 +253,11 @@ private static void runTimestamp() throws InterruptedException { } if (response.hasErrors()) { - throw new RuntimeException( - response - .getExecutionErrors() - .stream() - .map(err -> err.getMessage()) - .collect(Collectors.joining("\n"))); + String firstError = ""; + if (response.getExecutionErrors().size() != 0) { + firstError = response.getExecutionErrors().get(0).getMessage(); + } + throw new RuntimeException(firstError); } QueryResult result = response.getResult(); diff --git a/bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java b/bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java index 9cde1155d80..94ccd8edece 100644 --- a/bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java +++ b/bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java @@ -27,7 +27,6 @@ import java.io.PrintStream; import java.util.Iterator; import java.util.List; -import java.util.stream.Collectors; /** * Runs a synchronous query against BigQuery. @@ -40,7 +39,7 @@ public class SyncQuerySample { /** * Prompts the user for the required parameters to perform a query. */ - public static void main(final String[] args) throws IOException { + public static void main(final String[] args) throws IOException, InterruptedException { String queryString = System.getProperty("query"); if (queryString == null || queryString.isEmpty()) { System.out.println("The query property was not set, using default."); @@ -82,7 +81,7 @@ public static void run( final PrintStream out, final String queryString, final long waitTime, - final boolean useLegacySql) throws IOException { + final boolean useLegacySql) throws IOException, InterruptedException { BigQuery bigquery = new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance()); @@ -95,20 +94,28 @@ public static void run( .build(); 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()); + } + if (response.hasErrors()) { - throw new RuntimeException( - response - .getExecutionErrors() - .stream() - .map(err -> err.getMessage()) - .collect(Collectors.joining("\n"))); + String firstError = ""; + if (response.getExecutionErrors().size() != 0) { + firstError = response.getExecutionErrors().get(0).getMessage(); + } + throw new RuntimeException(firstError); } QueryResult result = response.getResult(); Iterator> iter = result.iterateAll(); while (iter.hasNext()) { List row = iter.next(); - out.println(row.stream().map(val -> val.toString()).collect(Collectors.joining(","))); + for (FieldValue val : row) { + out.printf("%s,", val.toString()); + } + out.printf("\n"); } } // [END run] diff --git a/bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleIT.java b/bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleIT.java index 38359596e21..fff489c9e86 100644 --- a/bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleIT.java +++ b/bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleIT.java @@ -24,7 +24,6 @@ import org.junit.runners.JUnit4; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.io.PrintStream; /** @@ -43,7 +42,7 @@ public void setUp() { } @Test - public void testSyncQuery() throws IOException { + public void testSyncQuery() throws Exception { SyncQuerySample.run( out, "SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;", @@ -55,7 +54,7 @@ public void testSyncQuery() throws IOException { } @Test - public void testSyncQueryLegacySql() throws IOException { + public void testSyncQueryLegacySql() throws Exception { SyncQuerySample.run( out, "SELECT corpus FROM [publicdata:samples.shakespeare] GROUP BY corpus;",