30
30
31
31
import java .io .IOException ;
32
32
import java .util .Arrays ;
33
- import java .util .Iterator ;
34
33
import java .util .List ;
35
34
36
35
/** A sample that demonstrates use of query parameters. */
@@ -141,6 +140,7 @@ private static void runNamed(final String corpus, final long minWordCount)
141
140
response = bigquery .getQueryResults (response .getJobId ());
142
141
}
143
142
143
+ // Check for errors.
144
144
if (response .hasErrors ()) {
145
145
String firstError = "" ;
146
146
if (response .getExecutionErrors ().size () != 0 ) {
@@ -149,12 +149,14 @@ private static void runNamed(final String corpus, final long minWordCount)
149
149
throw new RuntimeException (firstError );
150
150
}
151
151
152
+ // Print all pages of the results.
152
153
QueryResult result = response .getResult ();
153
- Iterator <List <FieldValue >> iter = result .iterateAll ();
154
+ while (result != null ) {
155
+ for (List <FieldValue > row : result .iterateAll ()) {
156
+ System .out .printf ("%s: %d\n " , row .get (0 ).getStringValue (), row .get (1 ).getLongValue ());
157
+ }
154
158
155
- while (iter .hasNext ()) {
156
- List <FieldValue > row = iter .next ();
157
- System .out .printf ("%s: %d\n " , row .get (0 ).getStringValue (), row .get (1 ).getLongValue ());
159
+ result = result .getNextPage ();
158
160
}
159
161
}
160
162
// [END bigquery_query_params]
@@ -193,6 +195,7 @@ private static void runArray(String gender, String[] states) throws InterruptedE
193
195
response = bigquery .getQueryResults (response .getJobId ());
194
196
}
195
197
198
+ // Check for errors.
196
199
if (response .hasErrors ()) {
197
200
String firstError = "" ;
198
201
if (response .getExecutionErrors ().size () != 0 ) {
@@ -201,12 +204,14 @@ private static void runArray(String gender, String[] states) throws InterruptedE
201
204
throw new RuntimeException (firstError );
202
205
}
203
206
207
+ // Print all pages of the results.
204
208
QueryResult result = response .getResult ();
205
- Iterator <List <FieldValue >> iter = result .iterateAll ();
209
+ while (result != null ) {
210
+ for (List <FieldValue > row : result .iterateAll ()) {
211
+ System .out .printf ("%s: %d\n " , row .get (0 ).getStringValue (), row .get (1 ).getLongValue ());
212
+ }
206
213
207
- while (iter .hasNext ()) {
208
- List <FieldValue > row = iter .next ();
209
- System .out .printf ("%s: %d\n " , row .get (0 ).getStringValue (), row .get (1 ).getLongValue ());
214
+ result = result .getNextPage ();
210
215
}
211
216
}
212
217
// [END bigquery_query_params_arrays]
@@ -240,6 +245,7 @@ private static void runTimestamp() throws InterruptedException {
240
245
response = bigquery .getQueryResults (response .getJobId ());
241
246
}
242
247
248
+ // Check for errors.
243
249
if (response .hasErrors ()) {
244
250
String firstError = "" ;
245
251
if (response .getExecutionErrors ().size () != 0 ) {
@@ -248,19 +254,21 @@ private static void runTimestamp() throws InterruptedException {
248
254
throw new RuntimeException (firstError );
249
255
}
250
256
257
+ // Print all pages of the results.
251
258
QueryResult result = response .getResult ();
252
- Iterator <List <FieldValue >> iter = result .iterateAll ();
253
-
254
259
DateTimeFormatter formatter = ISODateTimeFormat .dateTimeNoMillis ().withZoneUTC ();
255
- while (iter .hasNext ()) {
256
- List <FieldValue > row = iter .next ();
257
- System .out .printf (
258
- "%s\n " ,
259
- formatter .print (
260
- new DateTime (
261
- // Timestamp values are returned in microseconds since 1970-01-01T00:00:00 UTC,
262
- // but org.joda.time.DateTime constructor accepts times in milliseconds.
263
- row .get (0 ).getTimestampValue () / 1000 , DateTimeZone .UTC )));
260
+ while (result != null ) {
261
+ for (List <FieldValue > row : result .iterateAll ()) {
262
+ System .out .printf (
263
+ "%s\n " ,
264
+ formatter .print (
265
+ new DateTime (
266
+ // Timestamp values are returned in microseconds since 1970-01-01T00:00:00 UTC,
267
+ // but org.joda.time.DateTime constructor accepts times in milliseconds.
268
+ row .get (0 ).getTimestampValue () / 1000 , DateTimeZone .UTC )));
269
+ }
270
+
271
+ result = result .getNextPage ();
264
272
}
265
273
}
266
274
// [END bigquery_query_params_timestamps]
0 commit comments