Skip to content

Commit 10fc75a

Browse files
tswastlesv
authored andcommitted
Use Java 7 for BQ samples. (#586)
1 parent c5fd101 commit 10fc75a

File tree

4 files changed

+36
-34
lines changed

4 files changed

+36
-34
lines changed

bigquery/cloud-client/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
</parent>
2929

3030
<properties>
31-
<maven.compiler.target>1.8</maven.compiler.target>
32-
<maven.compiler.source>1.8</maven.compiler.source>
31+
<maven.compiler.target>1.7</maven.compiler.target>
32+
<maven.compiler.source>1.7</maven.compiler.source>
3333
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
3434
</properties>
3535

bigquery/cloud-client/src/main/java/com/example/bigquery/QueryParametersSample.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.Arrays;
3333
import java.util.Iterator;
3434
import java.util.List;
35-
import java.util.stream.Collectors;
3635

3736
/**
3837
* A sample that demonstrates use of query parameters.
@@ -150,12 +149,11 @@ private static void runNamed(final String corpus, final long minWordCount)
150149
}
151150

152151
if (response.hasErrors()) {
153-
throw new RuntimeException(
154-
response
155-
.getExecutionErrors()
156-
.stream()
157-
.<String>map(err -> err.getMessage())
158-
.collect(Collectors.joining("\n")));
152+
String firstError = "";
153+
if (response.getExecutionErrors().size() != 0) {
154+
firstError = response.getExecutionErrors().get(0).getMessage();
155+
}
156+
throw new RuntimeException(firstError);
159157
}
160158

161159
QueryResult result = response.getResult();
@@ -208,12 +206,11 @@ private static void runArray(String gender, String[] states)
208206
}
209207

210208
if (response.hasErrors()) {
211-
throw new RuntimeException(
212-
response
213-
.getExecutionErrors()
214-
.stream()
215-
.<String>map(err -> err.getMessage())
216-
.collect(Collectors.joining("\n")));
209+
String firstError = "";
210+
if (response.getExecutionErrors().size() != 0) {
211+
firstError = response.getExecutionErrors().get(0).getMessage();
212+
}
213+
throw new RuntimeException(firstError);
217214
}
218215

219216
QueryResult result = response.getResult();
@@ -256,12 +253,11 @@ private static void runTimestamp() throws InterruptedException {
256253
}
257254

258255
if (response.hasErrors()) {
259-
throw new RuntimeException(
260-
response
261-
.getExecutionErrors()
262-
.stream()
263-
.<String>map(err -> err.getMessage())
264-
.collect(Collectors.joining("\n")));
256+
String firstError = "";
257+
if (response.getExecutionErrors().size() != 0) {
258+
firstError = response.getExecutionErrors().get(0).getMessage();
259+
}
260+
throw new RuntimeException(firstError);
265261
}
266262

267263
QueryResult result = response.getResult();

bigquery/cloud-client/src/main/java/com/example/bigquery/SyncQuerySample.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.PrintStream;
2828
import java.util.Iterator;
2929
import java.util.List;
30-
import java.util.stream.Collectors;
3130

3231
/**
3332
* Runs a synchronous query against BigQuery.
@@ -40,7 +39,7 @@ public class SyncQuerySample {
4039
/**
4140
* Prompts the user for the required parameters to perform a query.
4241
*/
43-
public static void main(final String[] args) throws IOException {
42+
public static void main(final String[] args) throws IOException, InterruptedException {
4443
String queryString = System.getProperty("query");
4544
if (queryString == null || queryString.isEmpty()) {
4645
System.out.println("The query property was not set, using default.");
@@ -82,7 +81,7 @@ public static void run(
8281
final PrintStream out,
8382
final String queryString,
8483
final long waitTime,
85-
final boolean useLegacySql) throws IOException {
84+
final boolean useLegacySql) throws IOException, InterruptedException {
8685
BigQuery bigquery =
8786
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
8887

@@ -95,20 +94,28 @@ public static void run(
9594
.build();
9695
QueryResponse response = bigquery.query(queryRequest);
9796

97+
// Wait for the job to finish (if the query takes more than 10 seconds to complete).
98+
while (!response.jobCompleted()) {
99+
Thread.sleep(1000);
100+
response = bigquery.getQueryResults(response.getJobId());
101+
}
102+
98103
if (response.hasErrors()) {
99-
throw new RuntimeException(
100-
response
101-
.getExecutionErrors()
102-
.stream()
103-
.<String>map(err -> err.getMessage())
104-
.collect(Collectors.joining("\n")));
104+
String firstError = "";
105+
if (response.getExecutionErrors().size() != 0) {
106+
firstError = response.getExecutionErrors().get(0).getMessage();
107+
}
108+
throw new RuntimeException(firstError);
105109
}
106110

107111
QueryResult result = response.getResult();
108112
Iterator<List<FieldValue>> iter = result.iterateAll();
109113
while (iter.hasNext()) {
110114
List<FieldValue> row = iter.next();
111-
out.println(row.stream().map(val -> val.toString()).collect(Collectors.joining(",")));
115+
for (FieldValue val : row) {
116+
out.printf("%s,", val.toString());
117+
}
118+
out.printf("\n");
112119
}
113120
}
114121
// [END run]

bigquery/cloud-client/src/test/java/com/example/bigquery/SyncQuerySampleIT.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.runners.JUnit4;
2525

2626
import java.io.ByteArrayOutputStream;
27-
import java.io.IOException;
2827
import java.io.PrintStream;
2928

3029
/**
@@ -43,7 +42,7 @@ public void setUp() {
4342
}
4443

4544
@Test
46-
public void testSyncQuery() throws IOException {
45+
public void testSyncQuery() throws Exception {
4746
SyncQuerySample.run(
4847
out,
4948
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;",
@@ -55,7 +54,7 @@ public void testSyncQuery() throws IOException {
5554
}
5655

5756
@Test
58-
public void testSyncQueryLegacySql() throws IOException {
57+
public void testSyncQueryLegacySql() throws Exception {
5958
SyncQuerySample.run(
6059
out,
6160
"SELECT corpus FROM [publicdata:samples.shakespeare] GROUP BY corpus;",

0 commit comments

Comments
 (0)