Class: Google::Cloud::Bigquery::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/bigquery/job.rb,
lib/google/cloud/bigquery/job/list.rb

Overview

Job

Represents a generic Job that may be performed on a Table.

The subclasses of Job represent the specific BigQuery job types: CopyJob, ExtractJob, LoadJob, and QueryJob.

A job instance is created when you call Project#query_job, Dataset#query_job, Table#copy, Table#extract, Table#load, or View#data.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery

q = "SELECT COUNT(word) as count FROM publicdata:samples.shakespeare"
job = bigquery.query_job q

job.wait_until_done!

if job.failed?
  puts job.error
else
  puts job.query_results.first
end

See Also:

Direct Known Subclasses

CopyJob, ExtractJob, LoadJob, QueryJob

Defined Under Namespace

Classes: List

Instance Method Summary collapse

Instance Method Details

#configurationObject Also known as: config

The configuration for the job. Returns a hash.

See Also:



159
160
161
# File 'lib/google/cloud/bigquery/job.rb', line 159

def configuration
  JSON.parse @gapi.configuration.to_json
end

#created_atObject

The time when the job was created.



129
130
131
132
133
# File 'lib/google/cloud/bigquery/job.rb', line 129

def created_at
  Time.at(Integer(@gapi.statistics.creation_time) / 1000.0)
rescue
  nil
end

#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? to detect if an error occurred or if the job was successful.

Returns:

  • (Boolean)


116
117
118
119
# File 'lib/google/cloud/bigquery/job.rb', line 116

def done?
  return false if state.nil?
  "done".casecmp(state).zero?
end

#ended_atObject

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



148
149
150
151
152
# File 'lib/google/cloud/bigquery/job.rb', line 148

def ended_at
  Time.at(Integer(@gapi.statistics.end_time) / 1000.0)
rescue
  nil
end

#errorHash

The last error for the job, if any errors have occurred. Returns a hash.

Returns:

  • (Hash)

    Returns a hash containing reason and message keys:

    { "reason"=>"notFound", "message"=>"Not found: Table publicdata:samples.BAD_ID" }

See Also:



195
196
197
198
199
# File 'lib/google/cloud/bigquery/job.rb', line 195

def error
  return nil if @gapi.status.nil?
  return nil if @gapi.status.error_result.nil?
  JSON.parse @gapi.status.error_result.to_json
end

#errorsObject

The errors for the job, if any errors have occurred. Returns an array of hash objects. See #error.



204
205
206
207
# File 'lib/google/cloud/bigquery/job.rb', line 204

def errors
  return [] if @gapi.status.nil?
  Array(@gapi.status.errors).map { |e| JSON.parse e.to_json }
end

#failed?Boolean

Checks if an error is present.

Returns:

  • (Boolean)


123
124
125
# File 'lib/google/cloud/bigquery/job.rb', line 123

def failed?
  !error.nil?
end

#job_idObject

The ID of the job.



77
78
79
# File 'lib/google/cloud/bigquery/job.rb', line 77

def job_id
  @gapi.job_reference.job_id
end

#pending?Boolean

Checks if the job's state is PENDING.

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/google/cloud/bigquery/job.rb', line 106

def pending?
  return false if state.nil?
  "pending".casecmp(state).zero?
end

#project_idObject

The ID of the project containing the job.



83
84
85
# File 'lib/google/cloud/bigquery/job.rb', line 83

def project_id
  @gapi.job_reference.project_id
end

#reload!Object Also known as: refresh!

Reloads the job with current data from the BigQuery service.



219
220
221
222
223
# File 'lib/google/cloud/bigquery/job.rb', line 219

def reload!
  ensure_service!
  gapi = service.get_job job_id
  @gapi = gapi
end

#rerun!Object

Created a new job with the current configuration.



211
212
213
214
215
# File 'lib/google/cloud/bigquery/job.rb', line 211

def rerun!
  ensure_service!
  gapi = service.insert_job @gapi.configuration
  Job.from_gapi gapi, service
end

#running?Boolean

Checks if the job's state is RUNNING.

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/google/cloud/bigquery/job.rb', line 99

def running?
  return false if state.nil?
  "running".casecmp(state).zero?
end

#started_atObject

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



139
140
141
142
143
# File 'lib/google/cloud/bigquery/job.rb', line 139

def started_at
  Time.at(Integer(@gapi.statistics.start_time) / 1000.0)
rescue
  nil
end

#stateObject

The current state of the job. The possible values are PENDING, RUNNING, and DONE. A DONE state does not mean that the job completed successfully. Use #failed? to discover if an error occurred or if the job was successful.



92
93
94
95
# File 'lib/google/cloud/bigquery/job.rb', line 92

def state
  return nil if @gapi.status.nil?
  @gapi.status.state
end

#statisticsObject Also known as: stats

The statistics for the job. Returns a hash.

See Also:



169
170
171
# File 'lib/google/cloud/bigquery/job.rb', line 169

def statistics
  JSON.parse @gapi.statistics.to_json
end

#statusObject

The job's status. Returns a hash. The values contained in the hash are also exposed by #state, #error, and #errors.



177
178
179
# File 'lib/google/cloud/bigquery/job.rb', line 177

def status
  JSON.parse @gapi.status.to_json
end

#wait_until_done!Object

Refreshes the job until the job is DONE. The delay between refreshes will incrementally increase.

Examples:

require "google/cloud"

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

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


242
243
244
245
246
247
248
249
250
# File 'lib/google/cloud/bigquery/job.rb', line 242

def wait_until_done!
  backoff = ->(retries) { sleep 2 * retries + 5 }
  retries = 0
  until done?
    backoff.call retries
    retries += 1
    reload!
  end
end