Skip to content

Commit 4455cb7

Browse files
committed
Update BigQuery Samples.
1 parent 819b008 commit 4455cb7

File tree

3 files changed

+11
-65
lines changed

3 files changed

+11
-65
lines changed

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

Lines changed: 7 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import com.google.cloud.bigquery.FieldValue;
2222
import com.google.cloud.bigquery.QueryJobConfiguration;
2323
import com.google.cloud.bigquery.QueryParameterValue;
24-
import com.google.cloud.bigquery.QueryResponse;
25-
import com.google.cloud.bigquery.QueryResult;
24+
import com.google.cloud.bigquery.TableResult;
2625
import java.io.IOException;
2726
import java.util.Arrays;
2827
import java.util.List;
@@ -112,8 +111,7 @@ public static void main(final String[] args) throws IOException, InterruptedExce
112111
// [START bigquery_query_params]
113112
private static void runNamed(final String corpus, final long minWordCount)
114113
throws InterruptedException {
115-
BigQuery bigquery =
116-
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
114+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
117115

118116
String queryString =
119117
"SELECT word, word_count\n"
@@ -131,25 +129,9 @@ private static void runNamed(final String corpus, final long minWordCount)
131129
.build();
132130

133131
// Execute the query.
134-
QueryResponse response = bigquery.query(queryRequest);
135-
136-
// Wait for the job to finish (if the query takes more than 10 seconds to complete).
137-
while (!response.jobCompleted()) {
138-
Thread.sleep(1000);
139-
response = bigquery.getQueryResults(response.getJobId());
140-
}
141-
142-
// Check for errors.
143-
if (response.hasErrors()) {
144-
String firstError = "";
145-
if (response.getExecutionErrors().size() != 0) {
146-
firstError = response.getExecutionErrors().get(0).getMessage();
147-
}
148-
throw new RuntimeException(firstError);
149-
}
132+
TableResult result = bigquery.query(queryRequest);
150133

151134
// Print all pages of the results.
152-
QueryResult result = response.getResult();
153135
while (result != null) {
154136
for (List<FieldValue> row : result.iterateAll()) {
155137
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)
165147
*/
166148
// [START bigquery_query_params_arrays]
167149
private static void runArray(String gender, String[] states) throws InterruptedException {
168-
BigQuery bigquery =
169-
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
150+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
170151

171152
String queryString =
172153
"SELECT name, sum(number) as count\n"
@@ -186,25 +167,9 @@ private static void runArray(String gender, String[] states) throws InterruptedE
186167
.build();
187168

188169
// Execute the query.
189-
QueryResponse response = bigquery.query(queryRequest);
190-
191-
// Wait for the job to finish (if the query takes more than 10 seconds to complete).
192-
while (!response.jobCompleted()) {
193-
Thread.sleep(1000);
194-
response = bigquery.getQueryResults(response.getJobId());
195-
}
196-
197-
// Check for errors.
198-
if (response.hasErrors()) {
199-
String firstError = "";
200-
if (response.getExecutionErrors().size() != 0) {
201-
firstError = response.getExecutionErrors().get(0).getMessage();
202-
}
203-
throw new RuntimeException(firstError);
204-
}
170+
TableResult result = bigquery.query(queryRequest);
205171

206172
// Print all pages of the results.
207-
QueryResult result = response.getResult();
208173
while (result != null) {
209174
for (List<FieldValue> row : result.iterateAll()) {
210175
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
217182

218183
// [START bigquery_query_params_timestamps]
219184
private static void runTimestamp() throws InterruptedException {
220-
BigQuery bigquery =
221-
new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance());
185+
BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService();
222186

223187
DateTime timestamp = new DateTime(2016, 12, 7, 8, 0, 0, DateTimeZone.UTC);
224188

@@ -236,25 +200,9 @@ private static void runTimestamp() throws InterruptedException {
236200
.build();
237201

238202
// Execute the query.
239-
QueryResponse response = bigquery.query(queryRequest);
240-
241-
// Wait for the job to finish (if the query takes more than 10 seconds to complete).
242-
while (!response.jobCompleted()) {
243-
Thread.sleep(1000);
244-
response = bigquery.getQueryResults(response.getJobId());
245-
}
246-
247-
// Check for errors.
248-
if (response.hasErrors()) {
249-
String firstError = "";
250-
if (response.getExecutionErrors().size() != 0) {
251-
firstError = response.getExecutionErrors().get(0).getMessage();
252-
}
253-
throw new RuntimeException(firstError);
254-
}
203+
TableResult result = bigquery.query(queryRequest);
255204

256205
// Print all pages of the results.
257-
QueryResult result = response.getResult();
258206
DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC();
259207
while (result != null) {
260208
for (List<FieldValue> row : result.iterateAll()) {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
import com.google.cloud.bigquery.JobId;
2424
import com.google.cloud.bigquery.JobInfo;
2525
import com.google.cloud.bigquery.QueryJobConfiguration;
26-
import com.google.cloud.bigquery.QueryResponse;
27-
import com.google.cloud.bigquery.QueryResult;
2826
import com.google.cloud.bigquery.TableId;
27+
import com.google.cloud.bigquery.TableResult;
2928
import java.io.IOException;
3029
import java.util.List;
3130
import java.util.UUID;
@@ -137,8 +136,7 @@ public static void runQuery(QueryJobConfiguration queryConfig)
137136
}
138137

139138
// Get the results.
140-
QueryResponse response = bigquery.getQueryResults(jobId);
141-
QueryResult result = response.getResult();
139+
TableResult result = queryJob.getQueryResults();
142140

143141
// Print all pages of the results.
144142
while (result != null) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.google.cloud.bigquery.JobInfo;
2828
import com.google.cloud.bigquery.QueryJobConfiguration;
2929
import com.google.cloud.bigquery.QueryResponse;
30-
import com.google.cloud.bigquery.QueryResult;
30+
import com.google.cloud.bigquery.TableResult;
3131
import java.util.UUID;
3232
// [END bigquery_simple_app_deps]
3333

@@ -71,7 +71,7 @@ public static void main(String... args) throws Exception {
7171
// Get the results.
7272
QueryResponse response = bigquery.getQueryResults(jobId);
7373

74-
QueryResult result = response.getResult();
74+
TableResult result = queryJob.getQueryResults();
7575

7676
// Print all pages of the results.
7777
for (FieldValueList row : result.iterateAll()) {

0 commit comments

Comments
 (0)