Class Job (2.44.0)

public class Job extends JobInfo

A Google BigQuery Job.

Objects of this class are immutable. To get a Job object with the most recent information use #reload. Job adds a layer of service-related functionality over JobInfo.

Inheritance

Object > JobInfo > Job

Methods

cancel()

public boolean cancel()

Sends a job cancel request.

Example of cancelling a job.


 if (job.cancel()) {
   return true; // job successfully cancelled
 } else {
   // job not found
 }
 
Returns
Type Description
boolean

true if cancel request was sent successfully, false if job was not found

equals(Object obj)

public final boolean equals(Object obj)
Parameter
Name Description
obj Object
Returns
Type Description
boolean
Overrides

exists()

public boolean exists()

Checks if this job exists.

Example of checking that a job exists.


 if (!job.exists()) {
   // job doesn't exist
 }
 
Returns
Type Description
boolean

true if this job exists, false otherwise

getBigQuery()

public BigQuery getBigQuery()

Returns the job's BigQuery object used to issue requests.

Returns
Type Description
BigQuery

getQueryResults(BigQuery.QueryResultsOption[] options)

public TableResult getQueryResults(BigQuery.QueryResultsOption[] options)

Gets the query results of this job. This job must be of type JobConfiguration.Type.QUERY, otherwise this method will throw UnsupportedOperationException.

If the job hasn't finished, this method waits for the job to complete. However, the state of the current Job instance is not updated. To get the new state, call #waitFor(RetryOption...) or #reload(JobOption...).

Parameter
Name Description
options QueryResultsOption[]
Returns
Type Description
TableResult
Exceptions
Type Description
InterruptedException

upon failure

JobException

upon failure

hashCode()

public final int hashCode()
Returns
Type Description
int
Overrides

isDone()

public boolean isDone()

Checks if this job has completed its execution, either failing or succeeding. If the job does not exist this method returns true.

Example of waiting for a job until it reports that it is done.


 while (!job.isDone()) {
   Thread.sleep(1000L);
 }
 
Returns
Type Description
boolean

true if this job is in JobStatus.State#DONE state or if it does not exist, false if the state is not JobStatus.State#DONE

reload(BigQuery.JobOption[] options)

public Job reload(BigQuery.JobOption[] options)

Fetches current job's latest information. Returns null if the job does not exist.

Example of reloading all fields until job status is DONE.


 while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
   Thread.sleep(1000L);
   job = job.reload();
 }
 

Example of reloading status field until job status is DONE.


 while (!JobStatus.State.DONE.equals(job.getStatus().getState())) {
   Thread.sleep(1000L);
   job = job.reload(BigQuery.JobOption.fields(BigQuery.JobField.STATUS));
 }
 
Parameter
Name Description
options JobOption[]

job options

Returns
Type Description
Job

a Job object with latest information or null if not found

toBuilder()

public Job.Builder toBuilder()

Returns a builder for the job object.

Returns
Type Description
Job.Builder
Overrides

waitFor(RetryOption[] waitOptions)

public Job waitFor(RetryOption[] waitOptions)
Parameter
Name Description
waitOptions com.google.cloud.RetryOption[]
Returns
Type Description
Job
Exceptions
Type Description
InterruptedException

waitFor(BigQueryRetryConfig bigQueryRetryConfig, RetryOption[] waitOptions)

public Job waitFor(BigQueryRetryConfig bigQueryRetryConfig, RetryOption[] waitOptions)

Blocks until this job completes its execution, either failing or succeeding. This method returns current job's latest information. If the job no longer exists, this method returns null. By default, the job status is checked using jittered exponential backoff with 1 second as an initial delay, 2.0 as a backoff factor, 1 minute as maximum delay between polls, 12 hours as a total timeout and unlimited number of attempts. For query jobs, the job status check can be configured to retry on specific BigQuery error messages using BigQueryRetryConfig. This BigQueryRetryConfig configuration is not available for non-query jobs.

Example usage of waitFor().


 Job completedJob = job.waitFor();
 if (completedJob == null) {
   // job no longer exists
 } else if (completedJob.getStatus().getError() != null) {
   // job failed, handle error
 } else {
   // job completed successfully
 }
 

Example usage of waitFor() with checking period and timeout.


 Job completedJob =
     job.waitFor(
         RetryOption.initialRetryDelay(Duration.ofSeconds(1)),
         RetryOption.totalTimeout(Duration.ofMinutes(1)));
 if (completedJob == null) {
   // job no longer exists
 } else if (completedJob.getStatus().getError() != null) {
   // job failed, handle error
 } else {
   // job completed successfully
 }
 

Example usage of waitFor() with BigQuery retry configuration to retry on rate limit exceeded error messages for query jobs.


 Job completedJob =
     job.waitFor(
             BigQueryRetryConfig.newBuilder()
                 .retryOnMessage(BigQueryErrorMessages.RATE_LIMIT_EXCEEDED_MSG)
                 .retryOnMessage(BigQueryErrorMessages.JOB_RATE_LIMIT_EXCEEDED_MSG)
                 .retryOnRegEx(BigQueryErrorMessages.RetryRegExPatterns.RATE_LIMIT_EXCEEDED_REGEX)
                 .build());
 if (completedJob == null) {
   // job no longer exists
 } else if (completedJob.getStatus().getError() != null) {
   // job failed, handle error
 } else {
   // job completed successfully
 }
 
Parameters
Name Description
bigQueryRetryConfig BigQueryRetryConfig

configures retries for query jobs for BigQuery failures

waitOptions com.google.cloud.RetryOption[]

options to configure checking period and timeout

Returns
Type Description
Job
Exceptions
Type Description
InterruptedException

upon failure, check BigQueryException#getCause() for details