How to paginate results on a Bigquery query
You didn't post the query, but I guess you're looking for LIMIT 10 OFFSET 10
You can use Big Query JOBS to achieve it in two ways
String query="big query here...";
int startIndex=10;
int maxResults=10;
//fetches rows numbered 10 to 20 from result set
resultCollection = getPaginatedResultCollection(query, startIndex,maxResults);
//NOTE: Do what you want to do with paged data result i.e. resultCollection
/**
* Polls the status of a BigQuery job, returns TableReference to results if
* "DONE"
*/
private static TableReference checkQueryResults(Bigquery bigquery, String projectId, JobReference jobId) throws IOException, InterruptedException {
// Variables to keep track of total query time
while (true) {
Job pollJob = bigquery.jobs().get(projectId, jobId.getJobId()).execute();
if (pollJob.getStatus().getState().equals("DONE")) {
return pollJob.getConfiguration().getQuery().getDestinationTable();
}
// Pause execution for one second before polling job status again,
// to
// reduce unnecessary calls to the BigQUery API and lower overall
// application bandwidth.
// Thread.sleep(1000);
}
}
/**
* @param bigquery
* @param completedJob
* @param startIndex
* @param maxResultsPerPage
* @return
* @throws Exception
*/
private static ResultCollection displayQueryResults(Bigquery bigquery, TableReference completedJob, int startIndex, Integer maxResultsPerPage) throws Exception {
maxResultsPerPage = (maxResultsPerPage==null)? 20:maxResultsPerPage;
JSONObject responseMap = new JSONObject();
List<JSONObject> resultArray = new ArrayList<JSONObject>();
TableDataList queryResult = null;
queryResult = bigquery.tabledata().list(completedJob.getProjectId(), completedJob.getDatasetId(), completedJob.getTableId())
.setMaxResults(new Long(maxResultsPerPage))
.setStartIndex(BigInteger.valueOf(startIndex))
.execute();
//Table table = bigquery.tables().get(completedJob.getProjectId(), completedJob.getDatasetId(), completedJob.getTableId()).execute();
//NOTE: Schema can be read from table.getSchema().getFields()
if (CollectionUtils.isNotEmpty(queryResult.getRows())) {
//NOTE: read result data from queryResult.getRows() and transform the way you want to get them modeled, say resultCollection, for now
}
return resultCollection;
}