Enable large results

Query enables large result sets using legacy SQL.

Explore further

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

Code sample

Go

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

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

import("context""fmt""io""cloud.google.com/go/bigquery""google.golang.org/api/iterator")// queryLegacyLargeResults demonstrates issuing a legacy SQL query and writing a large result set// into a destination table.funcqueryLegacyLargeResults(wio.Writer,projectID,datasetID,tableIDstring)error{// projectID := "my-project-id"// datasetID := "destinationdataset"// tableID := "destinationtable"ctx:=context.Background()client,err:=bigquery.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("bigquery.NewClient: %w",err)}deferclient.Close()q:=client.Query("SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;")q.UseLegacySQL=trueq.AllowLargeResults=trueq.QueryConfig.Dst=client.Dataset(datasetID).Table(tableID)// Run the query and process the returned row iterator.it,err:=q.Read(ctx)iferr!=nil{returnfmt.Errorf("query.Read(): %w",err)}for{varrow[]bigquery.Valueerr:=it.Next(&row)iferr==iterator.Done{break}iferr!=nil{returnerr}fmt.Fprintln(w,row)}returnnil}

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 large results and save the results to a table.publicclassQueryLargeResults{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringdestinationDataset="MY_DESTINATION_DATASET_NAME";StringdestinationTable="MY_DESTINATION_TABLE_NAME";Stringquery="SELECT corpus FROM [bigquery-public-data:samples.shakespeare] GROUP BY corpus;";queryLargeResults(destinationDataset,destinationTable,query);}publicstaticvoidqueryLargeResults(StringdestinationDataset,StringdestinationTable,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();QueryJobConfigurationqueryConfig=// To use legacy SQL syntax, set useLegacySql to true.QueryJobConfiguration.newBuilder(query).setUseLegacySql(true)// Save the results of the query to a permanent table..setDestinationTable(TableId.of(destinationDataset,destinationTable))// Allow results larger than the maximum response size.// If true, a destination table must be set..setAllowLargeResults(true).build();TableResultresults=bigquery.query(queryConfig);results.iterateAll().forEach(row->row.forEach(val->System.out.printf("%s,",val.toString())));System.out.println("Query large results 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 libraryconst{BigQuery}=require('@google-cloud/bigquery');constbigquery=newBigQuery();asyncfunctionqueryLegacyLargeResults(){// Query enables large result sets./**   * TODO(developer): Uncomment the following lines before running the sample   */// const projectId = "my_project"// const datasetId = "my_dataset";// const tableId = "my_table";constquery='SELECT word FROM [bigquery-public-data:samples.shakespeare] LIMIT 10;';// For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/queryconstoptions={query:query,// Location must match that of the dataset(s) referenced// in the query and of the destination table.useLegacySql:true,allowLargeResult:true,destinationTable:{projectId:projectId,datasetId:datasetId,tableId:tableId,},};const[job]=awaitbigquery.createQueryJob(options);console.log(`Job${job.id} started.`);// Wait for the query to finishconst[rows]=awaitjob.getQueryResults();// Print the resultsconsole.log('Rows:');rows.forEach(row=>console.log(row));}

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()# TODO(developer): Set table_id to the ID of the destination table.# table_id = "your-project.your_dataset.your_table_name"# Set the destination table and use_legacy_sql to True to use# legacy SQL syntax.job_config=bigquery.QueryJobConfig(allow_large_results=True,destination=table_id,use_legacy_sql=True)sql="""    SELECT corpus    FROM [bigquery-public-data:samples.shakespeare]    GROUP BY corpus;"""# Start the query, passing in the extra configuration.client.query_and_wait(sql,job_config=job_config)# Make an API request and wait for the query to finish.print("Query results loaded to the table{}".format(table_id))

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.