Skip to content
Advertisement

How to properly iterate Bigquery TableResult in Java

I am trying to iterate the rows from TableResult using getValues() as below. if I use getValues(), it’s retrieving only the first page rows. I want to iterate all the rows using getValues() and NOT using iterateAll(). In the below code, the problem is its going infinite time. not ending. while(results.hasNextPage()) is not ending. what is the problem in the below code?

JavaScript

I have only 200,000 records in the source table. below is the out put and I forcefully stopped the process.

JavaScript

Advertisement

Answer

In short, you need to update TableResults variable with your getNextPage() variable. If you don’t update it you will always be looping the same results over and over. Thats why you are getting tons of records in your output.

If you check the following samples: Bigquery Pagination and Using Java Client Library. There are ways that we can deal with pagination results. Although not specific for single run queries.

As show on the code below, which is partially based on pagination sample, you need to use the output of getNextPage() to update results variable and proceed to perform the next iteration inside the while up until it iterates all pages but the last.

QueryRun.Java

JavaScript

output:

JavaScript

As a final note, there are not official sample about pagination for queries so I’m not totally sure of the recommended way to handle pagination with java. Its not quite clear on the BigQuery for Java documentation page. If you can update your question with your approach to pagination I would appreciate.

If you have issues running the attached sample please see Using the BigQuery Java client sample, its github page and its pom.xml file inside of it and check if you are in compliance with it.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement