3030
3131import java .io .IOException ;
3232import java .util .Arrays ;
33- import java .util .Iterator ;
3433import java .util .List ;
3534
3635/** A sample that demonstrates use of query parameters. */
@@ -141,6 +140,7 @@ private static void runNamed(final String corpus, final long minWordCount)
141140 response = bigquery .getQueryResults (response .getJobId ());
142141 }
143142
143+ // Check for errors.
144144 if (response .hasErrors ()) {
145145 String firstError = "" ;
146146 if (response .getExecutionErrors ().size () != 0 ) {
@@ -149,12 +149,14 @@ private static void runNamed(final String corpus, final long minWordCount)
149149 throw new RuntimeException (firstError );
150150 }
151151
152+ // Print all pages of the results.
152153 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+ }
154158
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 ();
158160 }
159161 }
160162 // [END bigquery_query_params]
@@ -193,6 +195,7 @@ private static void runArray(String gender, String[] states) throws InterruptedE
193195 response = bigquery .getQueryResults (response .getJobId ());
194196 }
195197
198+ // Check for errors.
196199 if (response .hasErrors ()) {
197200 String firstError = "" ;
198201 if (response .getExecutionErrors ().size () != 0 ) {
@@ -201,12 +204,14 @@ private static void runArray(String gender, String[] states) throws InterruptedE
201204 throw new RuntimeException (firstError );
202205 }
203206
207+ // Print all pages of the results.
204208 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+ }
206213
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 ();
210215 }
211216 }
212217 // [END bigquery_query_params_arrays]
@@ -240,6 +245,7 @@ private static void runTimestamp() throws InterruptedException {
240245 response = bigquery .getQueryResults (response .getJobId ());
241246 }
242247
248+ // Check for errors.
243249 if (response .hasErrors ()) {
244250 String firstError = "" ;
245251 if (response .getExecutionErrors ().size () != 0 ) {
@@ -248,19 +254,21 @@ private static void runTimestamp() throws InterruptedException {
248254 throw new RuntimeException (firstError );
249255 }
250256
257+ // Print all pages of the results.
251258 QueryResult result = response .getResult ();
252- Iterator <List <FieldValue >> iter = result .iterateAll ();
253-
254259 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 ();
264272 }
265273 }
266274 // [END bigquery_query_params_timestamps]
0 commit comments