Class: Google::Cloud::Bigquery::QueryJob

Inherits:
Job
  • Object
show all
Defined in:
lib/google/cloud/bigquery/query_job.rb

Overview

QueryJob

A Job subclass representing a query operation that may be performed on a Table. A QueryJob instance is created when you call Project#query_job, Dataset#query_job.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

job = bigquery.query_job "SELECT COUNT(word) as count FROM " \
                         "`bigquery-public-data.samples.shakespeare`"

job.wait_until_done!

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

See Also:

Direct Known Subclasses

Updater

Defined Under Namespace

Classes: Stage, Step, Updater

Attributes collapse

Instance Method Summary collapse

Methods inherited from Job

#cancel, #configuration, #created_at, #done?, #ended_at, #error, #errors, #failed?, #job_id, #labels, #location, #pending?, #project_id, #reload!, #rerun!, #running?, #started_at, #state, #statistics, #status, #user_email

Instance Method Details

#batch?Boolean

Checks if the priority for the query is BATCH.

Returns:

  • (Boolean)

    true when the priority is BATCH, false otherwise.



58
59
60
61
# File 'lib/google/cloud/bigquery/query_job.rb', line 58

def batch?
  val = @gapi.configuration.query.priority
  val == "BATCH"
end

#bytes_processedInteger

The number of bytes processed by the query.

Returns:

  • (Integer)

    Total bytes processed for the job.



159
160
161
162
163
# File 'lib/google/cloud/bigquery/query_job.rb', line 159

def bytes_processed
  Integer @gapi.statistics.query.total_bytes_processed
rescue StandardError
  nil
end

#cache?Boolean

Checks if the query job looks for an existing result in the query cache. For more information, see Query Caching.

Returns:

  • (Boolean)

    true when the query cache will be used, false otherwise.



96
97
98
99
100
# File 'lib/google/cloud/bigquery/query_job.rb', line 96

def cache?
  val = @gapi.configuration.query.use_query_cache
  return false if val.nil?
  val
end

#cache_hit?Boolean

Checks if the query results are from the query cache.

Returns:

  • (Boolean)

    true when the job statistics indicate a cache hit, false otherwise.



150
151
152
# File 'lib/google/cloud/bigquery/query_job.rb', line 150

def cache_hit?
  @gapi.statistics.query.cache_hit
end

#data(token: nil, max: nil, start: nil) ⇒ Google::Cloud::Bigquery::Data Also known as: query_results

Retrieves the query results for the job.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

sql = "SELECT word FROM `bigquery-public-data.samples.shakespeare`"
job = bigquery.query_job sql

job.wait_until_done!
data = job.data
data.each do |row|
  puts row[:word]
end
data = data.next if data.next?

Parameters:

  • token (String)

    Page token, returned by a previous call, identifying the result set.

  • max (Integer)

    Maximum number of results to return.

  • start (Integer)

    Zero-based index of the starting row to read.

Returns:



318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/google/cloud/bigquery/query_job.rb', line 318

def data token: nil, max: nil, start: nil
  return nil unless done?

  ensure_schema!

  options = { token: token, max: max, start: start }
  data_hash = service.list_tabledata \
    destination_table_dataset_id,
    destination_table_table_id,
    options
  Data.from_gapi_json data_hash, destination_table_gapi, service
end

#destinationTable

The table in which the query results are stored.

Returns:

  • (Table)

    A table instance.



202
203
204
205
206
207
208
# File 'lib/google/cloud/bigquery/query_job.rb', line 202

def destination
  table = @gapi.configuration.query.destination_table
  return nil unless table
  retrieve_table table.project_id,
                 table.dataset_id,
                 table.table_id
end

#encryptionGoogle::Cloud::BigQuery::EncryptionConfiguration

The encryption configuration of the destination table.

Returns:

  • (Google::Cloud::BigQuery::EncryptionConfiguration)

    Custom encryption configuration (e.g., Cloud KMS keys).



256
257
258
259
260
# File 'lib/google/cloud/bigquery/query_job.rb', line 256

def encryption
  EncryptionConfiguration.from_gapi(
    @gapi.configuration.query.destination_encryption_configuration
  )
end

#flatten?Boolean

Checks if the query job flattens nested and repeated fields in the query results. The default is true. If the value is false,

large_results? should return true.

Returns:

  • (Boolean)

    true when the job flattens results, false otherwise.



110
111
112
113
114
# File 'lib/google/cloud/bigquery/query_job.rb', line 110

def flatten?
  val = @gapi.configuration.query.flatten_results
  return true if val.nil?
  val
end

#interactive?Boolean

Checks if the priority for the query is INTERACTIVE.

Returns:

  • (Boolean)

    true when the priority is INTERACTIVE, false otherwise.



69
70
71
72
73
# File 'lib/google/cloud/bigquery/query_job.rb', line 69

def interactive?
  val = @gapi.configuration.query.priority
  return true if val.nil?
  val == "INTERACTIVE"
end

#large_results?Boolean

Checks if the the query job allows arbitrarily large results at a slight cost to performance.

Returns:

  • (Boolean)

    true when large results are allowed, false otherwise.



82
83
84
85
86
# File 'lib/google/cloud/bigquery/query_job.rb', line 82

def large_results?
  val = @gapi.configuration.query.allow_large_results
  return false if val.nil?
  val
end

#legacy_sql?Boolean

Checks if the query job is using legacy sql.

Returns:

  • (Boolean)

    true when legacy sql is used, false otherwise.



215
216
217
218
219
# File 'lib/google/cloud/bigquery/query_job.rb', line 215

def legacy_sql?
  val = @gapi.configuration.query.use_legacy_sql
  return true if val.nil?
  val
end

#maximum_billing_tierInteger?

Limits the billing tier for this job. Queries that have resource usage beyond this tier will raise (without incurring a charge). If unspecified, this will be set to your project default. For more information, see High-Compute queries.

Returns:

  • (Integer, nil)

    The tier number, or nil for the project default.



126
127
128
# File 'lib/google/cloud/bigquery/query_job.rb', line 126

def maximum_billing_tier
  @gapi.configuration.query.maximum_billing_tier
end

#maximum_bytes_billedInteger?

Limits the bytes billed for this job. Queries that will have bytes billed beyond this limit will raise (without incurring a charge). If nil, this will be set to your project default.

Returns:

  • (Integer, nil)

    The number of bytes, or nil for the project default.



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

def maximum_bytes_billed
  Integer @gapi.configuration.query.maximum_bytes_billed
rescue StandardError
  nil
end

#query_planArray<Google::Cloud::Bigquery::QueryJob::Stage>

Describes the execution plan for the query.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new

sql = "SELECT word FROM `bigquery-public-data.samples.shakespeare`"
job = bigquery.query_job sql

job.wait_until_done!

stages = job.query_plan
stages.each do |stage|
  puts stage.name
  stage.steps.each do |step|
    puts step.kind
    puts step.substeps.inspect
  end
end

Returns:



190
191
192
193
194
195
# File 'lib/google/cloud/bigquery/query_job.rb', line 190

def query_plan
  return nil unless @gapi.statistics.query.query_plan
  Array(@gapi.statistics.query.query_plan).map do |stage|
    Stage.from_gapi stage
  end
end

#standard_sql?Boolean

Checks if the query job is using standard sql.

Returns:

  • (Boolean)

    true when standard sql is used, false otherwise.



226
227
228
# File 'lib/google/cloud/bigquery/query_job.rb', line 226

def standard_sql?
  !legacy_sql?
end

#udfsArray<String>

The user-defined function resources used in the query. May be either a code resource to load from a Google Cloud Storage URI (gs://bucket/path), or an inline resource that contains code for a user-defined function (UDF). Providing an inline code resource is equivalent to providing a URI for a file containing the same code. See User-Defined Functions.

Returns:

  • (Array<String>)

    An array containing Google Cloud Storage URIs and/or inline source code.



241
242
243
244
245
246
247
# File 'lib/google/cloud/bigquery/query_job.rb', line 241

def udfs
  udfs_gapi = @gapi.configuration.query.user_defined_function_resources
  return nil unless udfs_gapi
  Array(udfs_gapi).map do |udf|
    udf.inline_code || udf.resource_uri
  end
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

sql = "SELECT word FROM `bigquery-public-data.samples.shakespeare`"
job = bigquery.query_job sql

job.wait_until_done!
job.done? #=> true


277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/google/cloud/bigquery/query_job.rb', line 277

def wait_until_done!
  return if done?

  ensure_service!
  loop do
    query_results_gapi = service.job_query_results \
      job_id, location: location, max: 0
    if query_results_gapi.job_complete
      @destination_schema_gapi = query_results_gapi.schema
      break
    end
  end
  reload!
end