forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncQuerySample.java
More file actions
115 lines (100 loc) · 3.96 KB
/
Copy pathSyncQuerySample.java
File metadata and controls
115 lines (100 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
Copyright 2016, Google, Inc.
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 com.google.cloud.bigquery.BigQuery;
import com.google.cloud.bigquery.BigQueryOptions;
import com.google.cloud.bigquery.FieldValue;
import com.google.cloud.bigquery.QueryRequest;
import com.google.cloud.bigquery.QueryResponse;
import com.google.cloud.bigquery.QueryResult;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
/**
* Runs a synchronous query against BigQuery.
*/
public class SyncQuerySample {
private static final String DEFAULT_QUERY =
"SELECT corpus FROM `publicdata.samples.shakespeare` GROUP BY corpus;";
private static final long TEN_SECONDS_MILLIS = 10000;
/**
* Prompts the user for the required parameters to perform a query.
*/
public static void main(final String[] args) throws IOException {
String queryString = System.getProperty("query");
if (queryString == null || queryString.isEmpty()) {
System.out.println("The query property was not set, using default.");
queryString = DEFAULT_QUERY;
}
System.out.printf("query: %s\n", queryString);
String waitTimeString = System.getProperty("waitTime");
if (waitTimeString == null || waitTimeString.isEmpty()) {
waitTimeString = "1000";
}
long waitTime = Long.parseLong(waitTimeString);
System.out.printf("waitTime: %d (milliseconds)\n", waitTime);
if (waitTime > TEN_SECONDS_MILLIS) {
System.out.println(
"WARNING: If the query is going to take longer than 10 seconds to complete, use an"
+ " asynchronous query.");
}
String useLegacySqlString = System.getProperty("useLegacySql");
if (useLegacySqlString == null || useLegacySqlString.isEmpty()) {
useLegacySqlString = "false";
}
boolean useLegacySql = Boolean.parseBoolean(useLegacySqlString);
run(System.out, queryString, waitTime, useLegacySql);
}
/**
* Perform the given query using the synchronous api.
*
* @param out stream to write results to
* @param queryString query to run
* @param waitTime Timeout in milliseconds before we abort
* @param useLegacySql Boolean that is false if using standard SQL syntax.
*/
// [START run]
public static void run(
final PrintStream out,
final String queryString,
final long waitTime,
final boolean useLegacySql) throws IOException {
BigQuery bigquery =
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
QueryRequest queryRequest =
QueryRequest.newBuilder(queryString)
.setMaxWaitTime(waitTime)
// Use standard SQL syntax or legacy SQL syntax for queries.
// See: https://cloud.google.com/bigquery/sql-reference/
.setUseLegacySql(useLegacySql)
.build();
QueryResponse response = bigquery.query(queryRequest);
if (response.hasErrors()) {
throw new RuntimeException(
response
.getExecutionErrors()
.stream()
.<String>map(err -> err.getMessage())
.collect(Collectors.joining("\n")));
}
QueryResult result = response.getResult();
Iterator<List<FieldValue>> iter = result.iterateAll();
while (iter.hasNext()) {
List<FieldValue> row = iter.next();
out.println(row.stream().map(val -> val.toString()).collect(Collectors.joining(",")));
}
}
// [END run]
}