Create a scheduled query Stay organized with collections Save and categorize content based on your preferences.
Schedule a query to run every 24 hours with a destination table identifier based on the run date.
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""cloud.google.com/go/bigquery"datatransfer"cloud.google.com/go/bigquery/datatransfer/apiv1""cloud.google.com/go/bigquery/datatransfer/apiv1/datatransferpb""google.golang.org/protobuf/types/known/structpb")// createScheduledQuery schedule a query to run every// 24 hours with a destination table identifier based on the run date.funccreateScheduledQuery(projectID,datasetID,querystring)error{// projectID := "my-project-id"// datasetID := "my-dataset-id"// query = `SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time,// @run_date as intended_run_date, 17 as some_integer`ctx:=context.Background()dtc,err:=datatransfer.NewClient(ctx)iferr!=nil{returnfmt.Errorf("datatransfer.NewClient: %w",err)}deferdtc.Close()paramsMap:=map[string]interface{}{}paramsMap["destination_table_name_template"]="my_destination_table_{run_date}"paramsMap["write_disposition"]=string(bigquery.WriteTruncate)paramsMap["partitioning_field"]=""paramsMap["query"]=queryparams,err:=structpb.NewStruct(paramsMap)iferr!=nil{returnfmt.Errorf("structpb.NewStruct: %w",err)}req:=&datatransferpb.CreateTransferConfigRequest{Parent:fmt.Sprintf("projects/%s",projectID),TransferConfig:&datatransferpb.TransferConfig{Destination:&datatransferpb.TransferConfig_DestinationDatasetId{DestinationDatasetId:datasetID,},DisplayName:"Your Scheduled Query Name",DataSourceId:"scheduled_query",Params:params,Schedule:"every 24 hours",},}_,err=dtc.CreateTransferConfig(ctx,req)iferr!=nil{returnfmt.Errorf("dtc.CreateTransferConfig: %w",err)}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.api.gax.rpc.ApiException;importcom.google.cloud.bigquery.datatransfer.v1.CreateTransferConfigRequest;importcom.google.cloud.bigquery.datatransfer.v1.DataTransferServiceClient;importcom.google.cloud.bigquery.datatransfer.v1.ProjectName;importcom.google.cloud.bigquery.datatransfer.v1.TransferConfig;importcom.google.protobuf.Struct;importcom.google.protobuf.Value;importjava.io.IOException;importjava.util.HashMap;importjava.util.Map;// Sample to create a scheduled querypublicclassCreateScheduledQuery{publicstaticvoidmain(String[]args)throwsIOException{// TODO(developer): Replace these variables before running the sample.finalStringprojectId="MY_PROJECT_ID";finalStringdatasetId="MY_DATASET_ID";finalStringquery="SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time, "+"@run_date as intended_run_date, 17 as some_integer";Map<String,Value>params=newHashMap<>();params.put("query",Value.newBuilder().setStringValue(query).build());params.put("destination_table_name_template",Value.newBuilder().setStringValue("my_destination_table_{run_date}").build());params.put("write_disposition",Value.newBuilder().setStringValue("WRITE_TRUNCATE").build());params.put("partitioning_field",Value.newBuilder().build());TransferConfigtransferConfig=TransferConfig.newBuilder().setDestinationDatasetId(datasetId).setDisplayName("Your Scheduled Query Name").setDataSourceId("scheduled_query").setParams(Struct.newBuilder().putAllFields(params).build()).setSchedule("every 24 hours").build();createScheduledQuery(projectId,transferConfig);}publicstaticvoidcreateScheduledQuery(StringprojectId,TransferConfigtransferConfig)throwsIOException{try(DataTransferServiceClientdataTransferServiceClient=DataTransferServiceClient.create()){ProjectNameparent=ProjectName.of(projectId);CreateTransferConfigRequestrequest=CreateTransferConfigRequest.newBuilder().setParent(parent.toString()).setTransferConfig(transferConfig).build();TransferConfigconfig=dataTransferServiceClient.createTransferConfig(request);System.out.println("\nScheduled query created successfully :"+config.getName());}catch(ApiExceptionex){System.out.print("\nScheduled query was not created."+ex.toString());}}}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_datatransfertransfer_client=bigquery_datatransfer.DataTransferServiceClient()# The project where the query job runs is the same as the project# containing the destination dataset.project_id="your-project-id"dataset_id="your_dataset_id"# This service account will be used to execute the scheduled queries. Omit# this request parameter to run the query as the user with the credentials# associated with this client.service_account_name="abcdef-test-sa@abcdef-test.iam.gserviceaccount.com"# Use standard SQL syntax for the query.query_string="""SELECT CURRENT_TIMESTAMP() as current_time, @run_time as intended_run_time, @run_date as intended_run_date, 17 as some_integer"""parent=transfer_client.common_project_path(project_id)transfer_config=bigquery_datatransfer.TransferConfig(destination_dataset_id=dataset_id,display_name="Your Scheduled Query Name",data_source_id="scheduled_query",params={"query":query_string,"destination_table_name_template":"your_table_{run_date}","write_disposition":"WRITE_TRUNCATE","partitioning_field":"",},schedule="every 24 hours",)transfer_config=transfer_client.create_transfer_config(bigquery_datatransfer.CreateTransferConfigRequest(parent=parent,transfer_config=transfer_config,service_account_name=service_account_name,))print("Created scheduled query '{}'".format(transfer_config.name))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.