Class: Google::Cloud::Bigquery::QueryData

Inherits:
Data
  • Object
show all
Defined in:
lib/google/cloud/bigquery/query_data.rb

Overview

QueryData

Represents Data returned from a query a a list of name/value pairs.

Instance Method Summary collapse

Methods inherited from Data

#etag, format_rows, format_values, #kind, #raw, #token, #total

Instance Method Details

#all(request_limit: nil) {|row| ... } ⇒ Enumerator

Retrieves all rows by repeatedly loading #next until #next? returns false. Calls the given block once for each row, which is passed as the parameter.

An Enumerator is returned if no block is given.

This method may make several API calls until all rows are retrieved. Be sure to use as narrow a search criteria as possible. Please use with caution.

Examples:

Iterating each row by passing a block:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
job = bigquery.job "my_job"

data = job.query_results
data.all do |row|
  puts row["word"]
end

Using the enumerator by not passing a block:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
job = bigquery.job "my_job"

data = job.query_results
words = data.all.map do |row|
  row["word"]
end

Limit the number of API calls made:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
job = bigquery.job "my_job"

data = job.query_results
data.all(request_limit: 10) do |row|
  puts row["word"]
end

Parameters:

  • request_limit (Integer)

    The upper limit of API requests to make to load all data. Default is no limit.

Yields:

  • (row)

    The block for accessing each row of data.

Yield Parameters:

  • row (Hash)

    The row object.

Returns:

  • (Enumerator)


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/google/cloud/bigquery/query_data.rb', line 175

def all request_limit: nil
  request_limit = request_limit.to_i if request_limit
  unless block_given?
    return enum_for(:all, request_limit: request_limit)
  end
  results = self
  loop do
    results.each { |r| yield r }
    if request_limit
      request_limit -= 1
      break if request_limit < 0
    end
    break unless results.next?
    results = results.next
  end
end

#cache_hit?Boolean

Whether the query result was fetched from the query cache.

Returns:

  • (Boolean)


51
52
53
# File 'lib/google/cloud/bigquery/query_data.rb', line 51

def cache_hit?
  @gapi.cache_hit
end

#complete?Boolean

Whether the query has completed or not. When data is present this will always be true. When false, total will not be available.

Returns:

  • (Boolean)


46
47
48
# File 'lib/google/cloud/bigquery/query_data.rb', line 46

def complete?
  @gapi.job_complete
end

#fieldsObject

The fields of the data.



63
64
65
66
67
68
# File 'lib/google/cloud/bigquery/query_data.rb', line 63

def fields
  f = schema.fields
  f = f.to_hash if f.respond_to? :to_hash
  f = [] if f.nil?
  f
end

#headersObject

The name of the columns in the data.



72
73
74
# File 'lib/google/cloud/bigquery/query_data.rb', line 72

def headers
  fields.map(&:name)
end

#jobObject

The BigQuery Job that was created to run the query.



194
195
196
197
198
199
200
201
202
# File 'lib/google/cloud/bigquery/query_data.rb', line 194

def job
  return @job if @job
  return nil unless job?
  ensure_service!
  gapi = service.get_job job_id
  @job = Job.from_gapi gapi, service
rescue Google::Cloud::NotFoundError
  nil
end

#nextQueryData

Retrieve the next page of query data.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
job = bigquery.job "my_job"

data = job.query_results
if data.next?
  next_data = data.next
end

Returns:



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

def next
  return nil unless next?
  ensure_service!
  gapi = service.job_query_results job_id, token: token
  QueryData.from_gapi gapi, service
end

#next?Boolean

Whether there is a next page of query data.

Examples:

require "google/cloud"

gcloud = Google::Cloud.new
bigquery = gcloud.bigquery
job = bigquery.job "my_job"

data = job.query_results
if data.next?
  next_data = data.next
end

Returns:

  • (Boolean)


93
94
95
# File 'lib/google/cloud/bigquery/query_data.rb', line 93

def next?
  !token.nil?
end

#schemaObject

The schema of the data.



57
58
59
# File 'lib/google/cloud/bigquery/query_data.rb', line 57

def schema
  Schema.from_gapi(@gapi.schema).freeze
end

#total_bytesObject

The total number of bytes processed for this query.



38
39
40
41
42
# File 'lib/google/cloud/bigquery/query_data.rb', line 38

def total_bytes
  Integer @gapi.total_bytes_processed
rescue
  nil
end