Query pagination

Run a query and get rows using automatic pagination.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Java

Before trying this sample, follow theJava setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryJava API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

importcom.google.cloud.bigquery.BigQuery;importcom.google.cloud.bigquery.BigQueryException;importcom.google.cloud.bigquery.BigQueryOptions;importcom.google.cloud.bigquery.QueryJobConfiguration;importcom.google.cloud.bigquery.TableId;importcom.google.cloud.bigquery.TableResult;// Sample to run query with pagination.publicclassQueryPagination{publicstaticvoidmain(String[]args){StringdatasetName="MY_DATASET_NAME";StringtableName="MY_TABLE_NAME";Stringquery="SELECT name, SUM(number) as total_people"+" FROM `bigquery-public-data.usa_names.usa_1910_2013`"+" GROUP BY name"+" ORDER BY total_people DESC"+" LIMIT 100";queryPagination(datasetName,tableName,query);}publicstaticvoidqueryPagination(StringdatasetName,StringtableName,Stringquery){try{// Initialize client that will be used to send requests. This client only needs to be created// once, and can be reused for multiple requests.BigQuerybigquery=BigQueryOptions.getDefaultInstance().getService();TableIdtableId=TableId.of(datasetName,tableName);QueryJobConfigurationqueryConfig=QueryJobConfiguration.newBuilder(query)// save results into a table..setDestinationTable(tableId).build();bigquery.query(queryConfig);TableResultresults=bigquery.listTableData(tableId,BigQuery.TableDataListOption.pageSize(20));// First Pageresults.getValues().forEach(row->row.forEach(val->System.out.printf("%s,\n",val.toString())));while(results.hasNextPage()){// Remaining Pagesresults=results.getNextPage();results.getValues().forEach(row->row.forEach(val->System.out.printf("%s,\n",val.toString())));}System.out.println("Query pagination performed successfully.");}catch(BigQueryException|InterruptedExceptione){System.out.println("Query not performed \n"+e.toString());}}}

Node.js

Before trying this sample, follow theNode.js setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryNode.js API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

// Import the Google Cloud client library using default credentialsconst{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();asyncfunctionqueryPagination(){// Run a query and get rows using automatic pagination.constquery=`SELECT name, SUM(number) as total_people  FROM \`bigquery-public-data.usa_names.usa_1910_2013\`  GROUP BY name  ORDER BY total_people DESC  LIMIT 100`;// Run the query as a job.const[job]=awaitbigquery.createQueryJob(query);// Wait for job to complete and get rows.// The client library automatically handles pagination.// See more info on how to configure paging calls at://  * https://github.com/googleapis/gax-nodejs/blob/main/client-libraries.md#auto-pagination//  * https://cloud.google.com/bigquery/docs/paging-results#iterate_through_client_libraries_resultsconst[rows]=awaitjob.getQueryResults();console.log('Query results:');rows.forEach(row=>{console.log(`name:${row.name},${row.total_people} total people`);});}queryPagination();

Python

Before trying this sample, follow thePython setup instructions in theBigQuery quickstart using client libraries. For more information, see theBigQueryPython API reference documentation.

To authenticate to BigQuery, set up Application Default Credentials. For more information, seeSet up authentication for client libraries.

fromgoogle.cloudimportbigquery# Construct a BigQuery client object.client=bigquery.Client()query="""    SELECT name, SUM(number) as total_people    FROM `bigquery-public-data.usa_names.usa_1910_2013`    GROUP BY name    ORDER BY total_people DESC"""query_job=client.query(query)# Make an API request.query_job.result()# Wait for the query to complete.# Get the destination table for the query results.## All queries write to a destination table. If a destination table is not# specified, the BigQuery populates it with a reference to a temporary# anonymous table after the query completes.destination=query_job.destination# Get the schema (and other properties) for the destination table.## A schema is useful for converting from BigQuery types to Python types.destination=client.get_table(destination)# Download rows.## The client library automatically handles pagination.print("The query data:")rows=client.list_rows(destination,max_results=20)forrowinrows:print("name={}, count={}".format(row["name"],row["total_people"]))

What's next

To search and filter code samples for other Google Cloud products, see theGoogle Cloud sample browser.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.