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/bigquery"

bigquery = Google::Cloud::Bigquery.new

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

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

#cancelObject

Cancels the job.



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

def cancel
  ensure_service!
  resp = service.cancel_job job_id
  @gapi = resp.job
  true
end

#configurationObject Also known as: config

The configuration for the job. Returns a hash.

See Also:



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

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

#created_atObject

The time when the job was created.



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

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)


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

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.



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

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:



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

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.



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

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)


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

def failed?
  !error.nil?
end

#job_idObject

The ID of the job.



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

def job_id
  @gapi.job_reference.job_id
end

#pending?Boolean

Checks if the job's state is PENDING.

Returns:

  • (Boolean)


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

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

#project_idObject

The ID of the project containing the job.



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

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.



227
228
229
230
231
# File 'lib/google/cloud/bigquery/job.rb', line 227

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

#rerun!Object

Created a new job with the current configuration.



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

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)


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

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.



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

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.



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

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:



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

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.



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

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/bigquery"

bigquery = Google::Cloud::Bigquery.new
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


249
250
251
252
253
254
255
256
257
# File 'lib/google/cloud/bigquery/job.rb', line 249

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