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.

See Managing Jobs, Datasets, and Projects for an overview of BigQuery jobs, and the Jobs API reference for details.

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.

require "gcloud"

gcloud = Gcloud.new
bigquery = gcloud.bigquery

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

loop do
  break if job.done?
  sleep 1
  job.refresh!
end

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

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

Create an empty Job object.



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

def initialize #:nodoc:
  @connection = nil
  @gapi = {}
end

Instance Attribute Details

#connectionObject

The Connection object.



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

def connection
  @connection
end

#gapiObject

The Google API Client object.



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

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi, conn) ⇒ Object

New Job from a Google API Client object.



234
235
236
237
238
239
240
# File 'lib/gcloud/bigquery/job.rb', line 234

def self.from_gapi gapi, conn #:nodoc:
  klass = klass_for gapi
  klass.new.tap do |f|
    f.gapi = gapi
    f.connection = conn
  end
end

Instance Method Details

#configurationObject Also known as: config

The configuration for the job. Returns a hash. See the Jobs API reference.



157
158
159
160
161
# File 'lib/gcloud/bigquery/job.rb', line 157

def configuration
  hash = @gapi["configuration"] || {}
  hash = hash.to_hash if hash.respond_to? :to_hash
  hash
end

#created_atObject

The time when the job was created.



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

def created_at
  return nil if @gapi["statistics"].nil?
  return nil if @gapi["statistics"]["creationTime"].nil?
  Time.at(@gapi["statistics"]["creationTime"] / 1000.0)
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/gcloud/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/gcloud/bigquery/job.rb', line 148

def ended_at
  return nil if @gapi["statistics"].nil?
  return nil if @gapi["statistics"]["endTime"].nil?
  Time.at(@gapi["statistics"]["endTime"] / 1000.0)
end

#errorObject

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

Returns

Hash

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


197
198
199
# File 'lib/gcloud/bigquery/job.rb', line 197

def error
  status["errorResult"]
end

#errorsObject

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



204
205
206
# File 'lib/gcloud/bigquery/job.rb', line 204

def errors
  Array status["errors"]
end

#failed?Boolean

Checks if an error is present.

Returns:

  • (Boolean)


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

def failed?
  !error.nil?
end

#job_idObject

The ID of the job.



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

def job_id
  @gapi["jobReference"]["jobId"]
end

#pending?Boolean

Checks if the job’s state is PENDING.

Returns:

  • (Boolean)


106
107
108
109
# File 'lib/gcloud/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/gcloud/bigquery/job.rb', line 83

def project_id
  @gapi["jobReference"]["projectId"]
end

#refresh!Object

Reloads the job with current data from the BigQuery service.



222
223
224
225
226
227
228
229
230
# File 'lib/gcloud/bigquery/job.rb', line 222

def refresh!
  ensure_connection!
  resp = connection.get_job job_id
  if resp.success?
    @gapi = resp.data
  else
    fail ApiError.from_response(resp)
  end
end

#rerun!Object

Created a new job with the current configuration.



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

def rerun!
  ensure_connection!
  resp = connection.insert_job configuration
  if resp.success?
    Job.from_gapi resp.data, connection
  else
    fail ApiError.from_response(resp)
  end
end

#running?Boolean

Checks if the job’s state is RUNNING.

Returns:

  • (Boolean)


99
100
101
102
# File 'lib/gcloud/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/gcloud/bigquery/job.rb', line 139

def started_at
  return nil if @gapi["statistics"].nil?
  return nil if @gapi["statistics"]["startTime"].nil?
  Time.at(@gapi["statistics"]["startTime"] / 1000.0)
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/gcloud/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 the Jobs API reference.



167
168
169
170
171
# File 'lib/gcloud/bigquery/job.rb', line 167

def statistics
  hash = @gapi["statistics"] || {}
  hash = hash.to_hash if hash.respond_to? :to_hash
  hash
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
180
181
# File 'lib/gcloud/bigquery/job.rb', line 177

def status
  hash = @gapi["status"] || {}
  hash = hash.to_hash if hash.respond_to? :to_hash
  hash
end