|
21 | 21 | import com.google.cloud.bigquery.FieldValue; |
22 | 22 | import com.google.cloud.bigquery.QueryJobConfiguration; |
23 | 23 | 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; |
26 | 25 | import java.io.IOException; |
27 | 26 | import java.util.Arrays; |
28 | 27 | import java.util.List; |
@@ -112,8 +111,7 @@ public static void main(final String[] args) throws IOException, InterruptedExce |
112 | 111 | // [START bigquery_query_params] |
113 | 112 | private static void runNamed(final String corpus, final long minWordCount) |
114 | 113 | throws InterruptedException { |
115 | | - BigQuery bigquery = |
116 | | - new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance()); |
| 114 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
117 | 115 |
|
118 | 116 | String queryString = |
119 | 117 | "SELECT word, word_count\n" |
@@ -131,25 +129,9 @@ private static void runNamed(final String corpus, final long minWordCount) |
131 | 129 | .build(); |
132 | 130 |
|
133 | 131 | // 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); |
150 | 133 |
|
151 | 134 | // Print all pages of the results. |
152 | | - QueryResult result = response.getResult(); |
153 | 135 | while (result != null) { |
154 | 136 | for (List<FieldValue> row : result.iterateAll()) { |
155 | 137 | 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) |
165 | 147 | */ |
166 | 148 | // [START bigquery_query_params_arrays] |
167 | 149 | 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(); |
170 | 151 |
|
171 | 152 | String queryString = |
172 | 153 | "SELECT name, sum(number) as count\n" |
@@ -186,25 +167,9 @@ private static void runArray(String gender, String[] states) throws InterruptedE |
186 | 167 | .build(); |
187 | 168 |
|
188 | 169 | // 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); |
205 | 171 |
|
206 | 172 | // Print all pages of the results. |
207 | | - QueryResult result = response.getResult(); |
208 | 173 | while (result != null) { |
209 | 174 | for (List<FieldValue> row : result.iterateAll()) { |
210 | 175 | 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 |
217 | 182 |
|
218 | 183 | // [START bigquery_query_params_timestamps] |
219 | 184 | private static void runTimestamp() throws InterruptedException { |
220 | | - BigQuery bigquery = |
221 | | - new BigQueryOptions.DefaultBigqueryFactory().create(BigQueryOptions.getDefaultInstance()); |
| 185 | + BigQuery bigquery = BigQueryOptions.getDefaultInstance().getService(); |
222 | 186 |
|
223 | 187 | DateTime timestamp = new DateTime(2016, 12, 7, 8, 0, 0, DateTimeZone.UTC); |
224 | 188 |
|
@@ -236,25 +200,9 @@ private static void runTimestamp() throws InterruptedException { |
236 | 200 | .build(); |
237 | 201 |
|
238 | 202 | // 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); |
255 | 204 |
|
256 | 205 | // Print all pages of the results. |
257 | | - QueryResult result = response.getResult(); |
258 | 206 | DateTimeFormatter formatter = ISODateTimeFormat.dateTimeNoMillis().withZoneUTC(); |
259 | 207 | while (result != null) { |
260 | 208 | for (List<FieldValue> row : result.iterateAll()) { |
|