Class: Gcloud::Bigquery::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/bigquery/job.rb,
lib/gcloud/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 "gcloud"

gcloud = Gcloud.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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJob

Returns a new instance of Job.



69
70
71
72
# File 'lib/gcloud/bigquery/job.rb', line 69

def initialize
  @service = nil
  @gapi = {}
end

Instance Attribute Details

#gapiObject



65
66
67
# File 'lib/gcloud/bigquery/job.rb', line 65

def gapi
  @gapi
end

#serviceObject



61
62
63
# File 'lib/gcloud/bigquery/job.rb', line 61

def service
  @service
end

Class Method Details

.from_gapi(gapi, conn) ⇒ Object



253
254
255
256
257
258
259
# File 'lib/gcloud/bigquery/job.rb', line 253

def self.from_gapi gapi, conn
  klass = klass_for gapi
  klass.new.tap do |f|
    f.gapi = gapi
    f.service = conn
  end
end

Instance Method Details

#configurationObject Also known as: config

The configuration for the job. Returns a hash.

See Also:



158
159
160
# File 'lib/gcloud/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/gcloud/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/gcloud/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/gcloud/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/gcloud/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/gcloud/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/gcloud/bigquery/job.rb', line 122

def failed?
  !error.nil?
end

#job_idObject

The ID of the job.



76
77
78
# File 'lib/gcloud/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/gcloud/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/gcloud/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.



218
219
220
221
222
# File 'lib/gcloud/bigquery/job.rb', line 218

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

#rerun!Object

Created a new job with the current configuration.



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

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

gcloud = Gcloud.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


241
242
243
244
245
246
247
248
249
# File 'lib/gcloud/bigquery/job.rb', line 241

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