1.64.1 (latest) 1.64.0 1.63.1 1.62.0 1.61.1 1.60.0 1.59.0 1.58.0 1.57.0 1.56.0 1.55.0 1.54.0 1.53.0 1.52.1 1.51.1 1.50.0 1.49.1 1.48.1 1.47.0 1.46.1 1.45.0 1.44.2 1.43.1 1.42.0 1.41.0 1.40.0 1.39.0 1.38.1 Reference documentation and code samples for the BigQuery API class Google::Cloud::Bigquery::Job.

## Job

Represents a generic Job that may be performed on a [Table](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Table "Google::Cloud::Bigquery::Table (class)").

The subclasses of Job represent the specific BigQuery job types:
[CopyJob](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-CopyJob "Google::Cloud::Bigquery::CopyJob (class)"), [ExtractJob](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-ExtractJob "Google::Cloud::Bigquery::ExtractJob (class)"), [LoadJob](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-LoadJob "Google::Cloud::Bigquery::LoadJob (class)"), and [QueryJob](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-QueryJob "Google::Cloud::Bigquery::QueryJob (class)").

A job instance is created when you call [Project#query_job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Project#Google__Cloud__Bigquery__Project_query_job_instance_ "Google::Cloud::Bigquery::Project#query_job (method)"),
[Dataset#query_job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Dataset#Google__Cloud__Bigquery__Dataset_query_job_instance_ "Google::Cloud::Bigquery::Dataset#query_job (method)"), [Table#copy_job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Table#Google__Cloud__Bigquery__Table_copy_job_instance_ "Google::Cloud::Bigquery::Table#copy_job (method)"), [Table#extract_job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Table#Google__Cloud__Bigquery__Table_extract_job_instance_ "Google::Cloud::Bigquery::Table#extract_job (method)"),
[Table#load_job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Table#Google__Cloud__Bigquery__Table_load_job_instance_ "Google::Cloud::Bigquery::Table#load_job (method)").

## Inherits

- Object

## Example

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

job = bigquery.query_job "SELECT COUNT(word) as count FROM " \
                         "`bigquery-public-data.samples.shakespeare`"

job.wait_until_done!

if job.failed?
  puts job.error
else
  puts job.data.first
end
```

## Methods

### #cancel

    def cancel()

Cancels the job.
**Example**

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

query = "SELECT COUNT(word) as count FROM " \
        "`bigquery-public-data.samples.shakespeare`"

job = bigquery.query_job query

job.cancel
```

### #config

    def config()

**Alias Of** : [#configuration](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_configuration_instance_) The configuration for the job. Returns a hash.

### #configuration

    def configuration()

**Aliases**

- [#config](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_config_instance_)
The configuration for the job. Returns a hash.

### #created_at

    def created_at() -> Time, nil

The time when the job was created.
**Returns**

- ([Time](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Time), nil) --- The creation time from the job statistics.

### #delete

    def delete() -> Boolean

Requests that a job is deleted. This call will return when the job is deleted.
**Returns**

- (Boolean) --- Returns `true` if the job was deleted.
**Example**

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

job = bigquery.job "my_job"

job.delete
```

### #done?

    def done?() -> Boolean

Checks if the job's state is `DONE`. When `true`, the job has stopped
running. However, a `DONE` state does not mean that the job completed
successfully. Use [#failed?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_failed__instance_ "Google::Cloud::Bigquery::Job#failed? (method)") to detect if an error occurred or if the
job was successful.
**Returns**

- (Boolean) --- `true` when `DONE`, `false` otherwise.

### #ended_at

    def ended_at() -> Time, nil

The time when the job ended.
This field is present when the job's state is `DONE`.
**Returns**

- ([Time](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Time), nil) --- The end time from the job statistics.

### #error

    def error() -> Hash, nil

Output only. Final error result of the job. If present, indicates that
the job has completed and was unsuccessful.
**Returns**

- (Hash, nil) --- Returns a hash containing `reason` and `message` keys:


  {
  "reason"=\>"notFound",
  "message"=\>"Not found: Table bigquery-public-data:samples.BAD_ID"
  }

### #errors

    def errors() -> Array<Hash>, nil

Output only. The first errors encountered during the running of the
job. The final message includes the number of errors that caused the
process to stop. Errors here do not necessarily mean that the job has
not completed or was unsuccessful.
**Returns**

- (Array\<Hash\>, nil) --- Returns an array of hashes containing `reason` and `message` keys:


  {
  "reason"=\>"notFound",
  "message"=\>"Not found: Table bigquery-public-data:samples.BAD_ID"
  }

### #failed?

    def failed?() -> Boolean

Checks if an error is present. Use [#error](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_error_instance_ "Google::Cloud::Bigquery::Job#error (method)") to access the error
object.
**Returns**

- (Boolean) --- `true` when there is an error, `false` otherwise.

### #job_id

    def job_id() -> String

The ID of the job.
**Returns**

- (String) --- The ID must contain only letters (`[A-Za-z]`), numbers (`[0-9]`), underscores (`_`), or dashes (`-`). The maximum length is 1,024 characters.

### #labels

    def labels() -> Hash

A hash of user-provided labels associated with this job. Labels can be
provided when the job is created, and used to organize and group jobs.


<br />

The returned hash is frozen and changes are not allowed. Use
[CopyJob::Updater#labels=](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-CopyJob-Updater#Google__Cloud__Bigquery__CopyJob__Updater_labels__instance_ "Google::Cloud::Bigquery::CopyJob::Updater#labels= (method)") or [ExtractJob::Updater#labels=](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-ExtractJob-Updater#Google__Cloud__Bigquery__ExtractJob__Updater_labels__instance_ "Google::Cloud::Bigquery::ExtractJob::Updater#labels= (method)") or
[LoadJob::Updater#labels=](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-LoadJob-Updater#Google__Cloud__Bigquery__LoadJob__Updater_labels__instance_ "Google::Cloud::Bigquery::LoadJob::Updater#labels= (method)") or [QueryJob::Updater#labels=](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-QueryJob-Updater#Google__Cloud__Bigquery__QueryJob__Updater_labels__instance_ "Google::Cloud::Bigquery::QueryJob::Updater#labels= (method)") to replace
the entire hash.
**Returns**

- (Hash) --- The job labels.

### #location

    def location() -> String

The geographic location where the job runs.
**Returns**

- (String) --- A geographic location, such as "US", "EU" or "asia-northeast1".

### #num_child_jobs

    def num_child_jobs() -> Integer

The number of child jobs executed.
**Returns**

- (Integer) --- The number of child jobs executed.

### #parent_job_id

    def parent_job_id() -> String, nil

If this is a child job, the id of the parent.
**Returns**

- (String, nil) --- The ID of the parent job, or `nil` if not a child job.

### #pending?

    def pending?() -> Boolean

Checks if the job's state is `PENDING`.
**Returns**

- (Boolean) --- `true` when `PENDING`, `false` otherwise.

### #project_id

    def project_id() -> String

The ID of the project containing the job.
**Returns**

- (String) --- The project ID.

### #refresh!

    def refresh!()

**Alias Of** : [#reload!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_reload!_instance_) Reloads the job with current data from the BigQuery service.
**Example**

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

query = "SELECT COUNT(word) as count FROM " \
        "`bigquery-public-data.samples.shakespeare`"

job = bigquery.query_job query

job.done?
job.reload!
job.done? #=> true
```

### #reload!

    def reload!()

**Aliases**

- [#refresh!](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_refresh!_instance_)
Reloads the job with current data from the BigQuery service.
**Example**

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

query = "SELECT COUNT(word) as count FROM " \
        "`bigquery-public-data.samples.shakespeare`"

job = bigquery.query_job query

job.done?
job.reload!
job.done? #=> true
```

### #rerun!

    def rerun!()

Created a new job with the current configuration.
**Example**

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

query = "SELECT COUNT(word) as count FROM " \
        "`bigquery-public-data.samples.shakespeare`"

job = bigquery.query_job query

job.wait_until_done!
job.rerun!
```

### #reservation_usage

    def reservation_usage() -> Array<Google::Cloud::Bigquery::Job::ReservationUsage>, nil

An array containing the job resource usage breakdown by reservation, if present. Reservation usage statistics
are only reported for jobs that are executed within reservations. On-demand jobs do not report this data.
**Returns**

- (Array\<[Google::Cloud::Bigquery::Job::ReservationUsage](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job-ReservationUsage)\>, nil) --- The reservation usage, if present.

### #running?

    def running?() -> Boolean

Checks if the job's state is `RUNNING`.
**Returns**

- (Boolean) --- `true` when `RUNNING`, `false` otherwise.

### #script_statistics

    def script_statistics() -> Google::Cloud::Bigquery::Job::ScriptStatistics, nil

The statistics including stack frames for a child job of a script.
**Returns**

- ([Google::Cloud::Bigquery::Job::ScriptStatistics](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job-ScriptStatistics), nil) --- The script statistics, or `nil` if the job is not a child job.
**Example**

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

multi_statement_sql = <<~SQL
  -- Declare a variable to hold names as an array.
  DECLARE top_names ARRAY<STRING>;
  -- Build an array of the top 100 names from the year 2017.
  SET top_names = (
  SELECT ARRAY_AGG(name ORDER BY number DESC LIMIT 100)
  FROM `bigquery-public-data.usa_names.usa_1910_current`
  WHERE year = 2017
  );
  -- Which names appear as words in Shakespeare's plays?
  SELECT
  name AS shakespeare_name
  FROM UNNEST(top_names) AS name
  WHERE name IN (
  SELECT word
  FROM `bigquery-public-data.samples.shakespeare`
  );
SQL

job = bigquery.query_job multi_statement_sql

job.wait_until_done!

child_jobs = bigquery.jobs parent_job: job

child_jobs.each do |child_job|
  script_statistics = child_job.script_statistics
  puts script_statistics.evaluation_kind
  script_statistics.stack_frames.each do |stack_frame|
    puts stack_frame.text
  end
end
```

### #session_id

    def session_id() -> String, nil

The ID of the session if this job is part of one. See the `create_session` param in [Project#query_job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Project#Google__Cloud__Bigquery__Project_query_job_instance_ "Google::Cloud::Bigquery::Project#query_job (method)") and
[Dataset#query_job](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Dataset#Google__Cloud__Bigquery__Dataset_query_job_instance_ "Google::Cloud::Bigquery::Dataset#query_job (method)").
**Returns**

- (String, nil) --- The session ID, or `nil` if not associated with a session.

### #started_at

    def started_at() -> Time, nil

The time when the job was started.
This field is present after the job's state changes from `PENDING`
to either `RUNNING` or `DONE`.
**Returns**

- ([Time](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Time), nil) --- The start time from the job statistics.

### #state

    def state() -> String

The current state of the job. A `DONE` state does not mean that the
job completed successfully. Use [#failed?](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_failed__instance_ "Google::Cloud::Bigquery::Job#failed? (method)") to discover if an error
occurred or if the job was successful.
**Returns**

- (String) --- The state code. The possible values are `PENDING`, `RUNNING`, and `DONE`.

### #statistics

    def statistics() -> Hash

**Aliases**

- [#stats](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_stats_instance_)
The statistics for the job. Returns a hash.
**Returns**

- (Hash) --- The job statistics.

### #stats

    def stats() -> Hash

**Alias Of** : [#statistics](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_statistics_instance_) The statistics for the job. Returns a hash.
**Returns**

- (Hash) --- The job statistics.

### #status

    def status() -> Hash

The job's status. Returns a hash. The values contained in the hash are
also exposed by [#state](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_state_instance_ "Google::Cloud::Bigquery::Job#state (method)"), [#error](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_error_instance_ "Google::Cloud::Bigquery::Job#error (method)"), and [#errors](https://docs.cloud.google.com/ruby/docs/reference/google-cloud-bigquery/latest/Google-Cloud-Bigquery-Job#Google__Cloud__Bigquery__Job_errors_instance_ "Google::Cloud::Bigquery::Job#errors (method)").
**Returns**

- (Hash) --- The job status.

### #transaction_id

    def transaction_id() -> String, nil

The ID of a multi-statement transaction.
**Returns**

- (String, nil) --- The transaction ID, or `nil` if not associated with a transaction.

### #user_email

    def user_email() -> String

The email address of the user who ran the job.
**Returns**

- (String) --- The email address.

### #wait_until_done!

    def wait_until_done!()

Refreshes the job until the job is `DONE`. The delay between refreshes
starts at 5 seconds and increases exponentially to a maximum of 60
seconds.
**Example**

```ruby
require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

extract_job = table.extract_job "gs://my-bucket/file-name.json",
                                format: "json"
extract_job.wait_until_done!
extract_job.done? #=> true
```