Running jobs programmatically
To run a BigQuery job programmatically using the REST API or client libraries, you:
- Call the
jobs.insert
method. - Periodically request the job resource and examine the status property to learn when the job is complete.
- Check to see whether the job finished successfully.
Before you begin
Grant Identity and Access Management (IAM) roles that give users the necessary permissions to perform each task in this document.
Required permissions
To run a BigQuery job, you need the bigquery.jobs.create
IAM permission.
Each of the following predefined IAM roles includes the permissions that you need in order to run a job:
roles/bigquery.user
roles/bigquery.jobUser
roles/bigquery.admin
Additionally, when you create a job, you are automatically granted the following permissions for that job:
bigquery.jobs.get
bigquery.jobs.update
For more information on IAM roles and permissions in BigQuery, see Predefined roles and permissions.
Running jobs
To run a job programmatically:
Start the job by calling the
jobs.insert
method. When you call thejobs.insert
method, include a job resource representation.In the
configuration
section of the job resource, include a child property that specifies the job type —load
,query
,extract
, orcopy
.After calling the
jobs.insert
method, check the job status by callingjobs.get
with the job ID and location, and check thestatus.state
value to learn the job status. Whenstatus.state
isDONE
, the job has stopped running; however, aDONE
status does not mean that the job completed successfully, only that it is no longer running.Check for job success. If the job has an
errorResult
property, the job has failed. Thestatus.errorResult
property holds information describing what went wrong in a failed job. Ifstatus.errorResult
is absent, the job finished successfully, although there might have been some nonfatal errors, such as problems importing a few rows in a load job. Nonfatal errors are returned in the job'sstatus.errors
list.
Running jobs using client libraries
To create and run a job using the Cloud Client Libraries for BigQuery:
C#
Before trying this sample, follow the C# setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery C# API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Java
Before trying this sample, follow the Java setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Java API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Python
Before trying this sample, follow the Python setup instructions in the BigQuery quickstart using client libraries. For more information, see the BigQuery Python API reference documentation.
To authenticate to BigQuery, set up Application Default Credentials. For more information, see Set up authentication for client libraries.
Adding job labels
Labels can be added to query jobs through the command line by using the
bq command-line tool's --label
flag. The bq tool supports adding
labels only to query jobs.
You can also add a label to a job when it's submitted through the API by specifying
the labels
property in the job configuration when you call the
jobs.insert
method. The API
can be used to add labels to any job type.
You cannot add labels to or update labels on pending, running, or completed jobs.
When you add a label to a job, the label is included in your billing data.
For more information, see Adding job labels.
What's next
- See Running queries for a code example that starts and polls a query job.
- For more information on creating a job resource representation, see the Jobs overview page in the API reference.