To run a BigQuery job programmatically using the REST API or client libraries, you:
- Call the
jobs.insert
method using a unique job ID that is generated by your client code - 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
Required permissions
At a minimum, to run BigQuery jobs, you must be granted
bigquery.jobs.create
permissions. The following predefined IAM
roles incude bigquery.jobs.create
permissions:
bigquery.user
bigquery.jobUser
bigquery.admin
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 that contains:- Your location in the
location
property in thejobReference
section. The job ID generated by your client code. The server generates a job ID for you if you omit it, but it is a best practice to generate the job ID on the client side to allow reliable retry of the
jobs.insert
call.For example:
{ "jobReference": { "projectId": "my_project", "jobId": "job_123", “location”: “asia-northeast1” }, "configuration": { // .. }, }
- Your location in the
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 non-fatal errors, such as problems importing a few rows in a load job. Non-fatal 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.
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.
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.
Generating a job ID
As a best practice, generate a job ID using your client code and send that job
ID when you call jobs.insert
. If you call jobs.insert
without specifying a
job ID, BigQuery will create a job ID for you, but you will not be
able to check the status of that job until the call returns. As well, it might
be difficult to tell if the job was successfully inserted. If you use your own
job ID, you can check the status of the job at any time, and you can retry on
the same job ID to ensure that the job starts exactly one time.
The job ID is a string comprising letters (a-z, A-Z), numbers (0-9), underscores (_), or dashes (-), with a maximum length of 1,024 characters. Job IDs must be unique within a project.
A common approach to generating a unique job ID is to use a human-readable
prefix and a suffix consisting of a timestamp or a GUID. For example:
daily_import_job_1447971251
.
An example of a method that generates GUIDs can be found in the
Python UUID module.
For an example of using the Python uuid4()
method with jobs.insert
,
see Loading data from Cloud Storage.
Next steps
- 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.