Class: Gcloud::Bigquery::Job
- Inherits:
-
Object
- Object
- Gcloud::Bigquery::Job
- 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
job.wait_until_done!
if job.failed?
puts job.error
else
puts job.query_results.first
end
Direct Known Subclasses
Defined Under Namespace
Classes: List
Instance Attribute Summary collapse
-
#connection ⇒ Object
The Connection object.
-
#gapi ⇒ Object
The Google API Client object.
Class Method Summary collapse
-
.from_gapi(gapi, conn) ⇒ Object
New Job from a Google API Client object.
Instance Method Summary collapse
-
#configuration ⇒ Object
(also: #config)
The configuration for the job.
-
#created_at ⇒ Object
The time when the job was created.
-
#done? ⇒ Boolean
Checks if the job’s state is
DONE. -
#ended_at ⇒ Object
The time when the job ended.
-
#error ⇒ Object
The last error for the job, if any errors have occurred.
-
#errors ⇒ Object
The errors for the job, if any errors have occurred.
-
#failed? ⇒ Boolean
Checks if an error is present.
-
#initialize ⇒ Job
constructor
Create an empty Job object.
-
#job_id ⇒ Object
The ID of the job.
-
#pending? ⇒ Boolean
Checks if the job’s state is
PENDING. -
#project_id ⇒ Object
The ID of the project containing the job.
-
#reload! ⇒ Object
(also: #refresh!)
Reloads the job with current data from the BigQuery service.
-
#rerun! ⇒ Object
Created a new job with the current configuration.
-
#running? ⇒ Boolean
Checks if the job’s state is
RUNNING. -
#started_at ⇒ Object
The time when the job was started.
-
#state ⇒ Object
The current state of the job.
-
#statistics ⇒ Object
(also: #stats)
The statistics for the job.
-
#status ⇒ Object
The job’s status.
-
#wait_until_done! ⇒ Object
Refreshes the job until the job is
DONE.
Constructor Details
#initialize ⇒ Job
Create an empty Job object.
66 67 68 69 |
# File 'lib/gcloud/bigquery/job.rb', line 66 def initialize #:nodoc: @connection = nil @gapi = {} end |
Instance Attribute Details
#connection ⇒ Object
The Connection object.
58 59 60 |
# File 'lib/gcloud/bigquery/job.rb', line 58 def connection @connection end |
#gapi ⇒ Object
The Google API Client object.
62 63 64 |
# File 'lib/gcloud/bigquery/job.rb', line 62 def gapi @gapi end |
Class Method Details
.from_gapi(gapi, conn) ⇒ Object
New Job from a Google API Client object.
258 259 260 261 262 263 264 |
# File 'lib/gcloud/bigquery/job.rb', line 258 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
#configuration ⇒ Object Also known as: config
The configuration for the job. Returns a hash. See the Jobs API reference.
153 154 155 156 157 |
# File 'lib/gcloud/bigquery/job.rb', line 153 def configuration hash = @gapi["configuration"] || {} hash = hash.to_hash if hash.respond_to? :to_hash hash end |
#created_at ⇒ Object
The time when the job was created.
125 126 127 128 129 |
# File 'lib/gcloud/bigquery/job.rb', line 125 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.
112 113 114 115 |
# File 'lib/gcloud/bigquery/job.rb', line 112 def done? return false if state.nil? "done".casecmp(state).zero? end |
#ended_at ⇒ Object
The time when the job ended. This field is present when the job’s state is DONE.
144 145 146 147 148 |
# File 'lib/gcloud/bigquery/job.rb', line 144 def ended_at return nil if @gapi["statistics"].nil? return nil if @gapi["statistics"]["endTime"].nil? Time.at(@gapi["statistics"]["endTime"] / 1000.0) end |
#error ⇒ Object
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"
}
193 194 195 |
# File 'lib/gcloud/bigquery/job.rb', line 193 def error status["errorResult"] end |
#errors ⇒ Object
The errors for the job, if any errors have occurred. Returns an array of hash objects. See #error.
200 201 202 |
# File 'lib/gcloud/bigquery/job.rb', line 200 def errors Array status["errors"] end |
#failed? ⇒ Boolean
Checks if an error is present.
119 120 121 |
# File 'lib/gcloud/bigquery/job.rb', line 119 def failed? !error.nil? end |
#job_id ⇒ Object
The ID of the job.
73 74 75 |
# File 'lib/gcloud/bigquery/job.rb', line 73 def job_id @gapi["jobReference"]["jobId"] end |
#pending? ⇒ Boolean
Checks if the job’s state is PENDING.
102 103 104 105 |
# File 'lib/gcloud/bigquery/job.rb', line 102 def pending? return false if state.nil? "pending".casecmp(state).zero? end |
#project_id ⇒ Object
The ID of the project containing the job.
79 80 81 |
# File 'lib/gcloud/bigquery/job.rb', line 79 def project_id @gapi["jobReference"]["projectId"] end |
#reload! ⇒ Object Also known as: refresh!
Reloads the job with current data from the BigQuery service.
218 219 220 221 222 223 224 225 226 |
# File 'lib/gcloud/bigquery/job.rb', line 218 def reload! 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.
206 207 208 209 210 211 212 213 214 |
# File 'lib/gcloud/bigquery/job.rb', line 206 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.
95 96 97 98 |
# File 'lib/gcloud/bigquery/job.rb', line 95 def running? return false if state.nil? "running".casecmp(state).zero? end |
#started_at ⇒ Object
The time when the job was started. This field is present after the job’s state changes from PENDING to either RUNNING or DONE.
135 136 137 138 139 |
# File 'lib/gcloud/bigquery/job.rb', line 135 def started_at return nil if @gapi["statistics"].nil? return nil if @gapi["statistics"]["startTime"].nil? Time.at(@gapi["statistics"]["startTime"] / 1000.0) end |
#state ⇒ Object
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.
88 89 90 91 |
# File 'lib/gcloud/bigquery/job.rb', line 88 def state return nil if @gapi["status"].nil? @gapi["status"]["state"] end |
#statistics ⇒ Object Also known as: stats
The statistics for the job. Returns a hash. See the Jobs API reference.
163 164 165 166 167 |
# File 'lib/gcloud/bigquery/job.rb', line 163 def statistics hash = @gapi["statistics"] || {} hash = hash.to_hash if hash.respond_to? :to_hash hash end |
#status ⇒ Object
The job’s status. Returns a hash. The values contained in the hash are also exposed by #state, #error, and #errors.
173 174 175 176 177 |
# File 'lib/gcloud/bigquery/job.rb', line 173 def status hash = @gapi["status"] || {} hash = hash.to_hash if hash.respond_to? :to_hash hash end |
#wait_until_done! ⇒ Object
Refreshes the job until the job is DONE. The delay between refreshes will incrementally increase.
Example
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
246 247 248 249 250 251 252 253 254 |
# File 'lib/gcloud/bigquery/job.rb', line 246 def wait_until_done! backoff = ->(retries) { sleep 2 * retries + 5 } retries = 0 until done? backoff.call retries retries += 1 reload! end end |