Get job properties

Retrieve the properties of a job for a given job ID.

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")// getJobInfo demonstrates retrieval of a job, which can be used to monitor// completion or print metadata about the job.funcgetJobInfo(wio.Writer,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{returnerr}status:=job.LastStatus()state:="Unknown"switchstatus.State{casebigquery.Pending:state="Pending"casebigquery.Running:state="Running"casebigquery.Done:state="Done"}fmt.Fprintf(w,"Job %s was created %v and is in state %s\n",jobID,status.Statistics.CreationTime,state)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.Job;importcom.google.cloud.bigquery.JobId;// Sample to get a jobpublicclassGetJob{publicstaticvoidmain(String[]args){// TODO(developer): Replace these variables before running the sample.StringjobName="MY_JOB_NAME";getJob(jobName);}publicstaticvoidgetJob(StringjobName){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();JobIdjobId=JobId.of(jobName);Jobjob=bigquery.getJob(jobId);System.out.println("Job retrieved successfully");}catch(BigQueryExceptione){System.out.println("Job not retrieved. \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();asyncfunctiongetJob(){// Get job properties./**   * TODO(developer): Uncomment the following lines before running the sample.   */// const jobId = "existing-job-id";// Create a job referenceconstjob=bigquery.job(jobId);// Retrieve jobconst[jobResult]=awaitjob.get();console.log(jobResult.metadata.jobReference);}

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.cloudimportbigquerydefget_job(client:bigquery.Client,location:str="us",job_id:str="abcd-efgh-ijkl-mnop",)->None:job=client.get_job(job_id,location=location)# All job classes have "location" and "job_id" string properties.# Use these properties for job operations such as "cancel_job" and# "delete_job".print(f"{job.location}:{job.job_id}")print(f"Type:{job.job_type}")print(f"State:{job.state}")print(f"Created:{job.created.isoformat()}")

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.