Cancel a job Stay organized with collections Save and categorize content based on your preferences.
Attempt to cancel a job.
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")// cancelJob demonstrates how a job cancellation request can be issued for a specific// BigQuery job.funccancelJob(projectID,jobIDstring)error{// projectID := "my-project-id"// jobID := "my-job-id"ctx:=context.Background()client,err:=bigquery.NewClient(ctx,projectID)iferr!=nil{returnfmt.Errorf("bigquery.NewClient: %w",err)}deferclient.Close()job,err:=client.JobFromID(ctx,jobID)iferr!=nil{returnnil}returnjob.Cancel(ctx)}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.Job;importcom.google.cloud.bigquery.JobId;importcom.google.cloud.bigquery.JobInfo;importcom.google.cloud.bigquery.QueryJobConfiguration;importjava.util.UUID;// Sample to cancel a jobpublicclassCancelJob{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.Stringquery="SELECT country_name from `bigquery-public-data.utility_us.country_code_iso`";cancelJob(query);}publicstaticvoidcancelJob(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();// Specify a job configuration to set optional job resource properties.QueryJobConfigurationqueryConfig=QueryJobConfiguration.newBuilder(query).build();// The location and job name are optional,// if both are not specified then client will auto-create.StringjobName="jobId_"+UUID.randomUUID().toString();JobIdjobId=JobId.newBuilder().setLocation("us").setJob(jobName).build();// Create a job with job IDbigquery.create(JobInfo.of(jobId,queryConfig));// Get a job that was just createdJobjob=bigquery.getJob(jobId);if(job.cancel()){System.out.println("Job canceled successfully");}else{System.out.println("Job was not canceled");}}catch(BigQueryExceptione){System.out.println("Job was not canceled.\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();asyncfunctioncancelJob(){// Attempts to cancel a job./** * TODO(developer): Uncomment the following lines before running the sample. */// const jobId = "existing-job-id";// Create a job referenceconstjob=bigquery.job(jobId);// Attempt to cancel jobconst[apiResult]=awaitjob.cancel();console.log(apiResult.job.status);}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.
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.