Class: Gcloud::Bigquery::QueryData
- Defined in:
- lib/gcloud/bigquery/query_data.rb
Overview
# QueryData
Represents Data returned from a query a a list of name/value pairs.
Instance Attribute Summary collapse
Attributes inherited from Data
Class Method Summary collapse
Instance Method Summary collapse
- #all(request_limit: nil) {|row| ... } ⇒ Enumerator
-
#cache_hit? ⇒ Boolean
Whether the query result was fetched from the query cache.
-
#complete? ⇒ Boolean
Whether the query has completed or not.
-
#fields ⇒ Object
The fields of the data.
-
#headers ⇒ Object
The name of the columns in the data.
-
#initialize(arr = []) ⇒ QueryData
constructor
A new instance of QueryData.
-
#job ⇒ Object
The BigQuery Job that was created to run the query.
-
#next ⇒ QueryData
Retrieve the next page of query data.
-
#next? ⇒ Boolean
Whether there is a next page of query data.
-
#schema ⇒ Object
The schema of the data.
-
#total_bytes ⇒ Object
The total number of bytes processed for this query.
Methods inherited from Data
#etag, format_rows, format_values, from_response, #kind, #raw, #token, #total
Constructor Details
#initialize(arr = []) ⇒ QueryData
Returns a new instance of QueryData.
30 31 32 33 |
# File 'lib/gcloud/bigquery/query_data.rb', line 30 def initialize arr = [] @job = nil super end |
Instance Attribute Details
#connection ⇒ Object
27 28 29 |
# File 'lib/gcloud/bigquery/query_data.rb', line 27 def connection @connection end |
Class Method Details
.from_gapi(gapi, connection) ⇒ Object
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/gcloud/bigquery/query_data.rb', line 210 def self.from_gapi gapi, connection if gapi["schema"].nil? formatted_rows = [] else formatted_rows = format_rows gapi["rows"], gapi["schema"]["fields"] end data = new formatted_rows data.gapi = gapi data.connection = connection data end |
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.
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/gcloud/bigquery/query_data.rb', line 178 def all request_limit: nil request_limit = request_limit.to_i if request_limit return enum_for(:all, request_limit: request_limit) unless block_given? 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.
47 48 49 |
# File 'lib/gcloud/bigquery/query_data.rb', line 47 def cache_hit? @gapi["cacheHit"] 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.
42 43 44 |
# File 'lib/gcloud/bigquery/query_data.rb', line 42 def complete? @gapi["jobComplete"] end |
#fields ⇒ Object
The fields of the data.
62 63 64 65 66 67 |
# File 'lib/gcloud/bigquery/query_data.rb', line 62 def fields f = schema["fields"] f = f.to_hash if f.respond_to? :to_hash f = [] if f.nil? f end |
#headers ⇒ Object
The name of the columns in the data.
71 72 73 |
# File 'lib/gcloud/bigquery/query_data.rb', line 71 def headers fields.map { |f| f["name"] } end |
#job ⇒ Object
The BigQuery Job that was created to run the query.
195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/gcloud/bigquery/query_data.rb', line 195 def job return @job if @job return nil unless job? ensure_connection! resp = connection.get_job job_id if resp.success? @job = Job.from_gapi resp.data, connection else return nil if resp.status == 404 fail ApiError.from_response(resp) end end |
#next ⇒ QueryData
Retrieve the next page of query data.
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/gcloud/bigquery/query_data.rb', line 113 def next return nil unless next? ensure_connection! resp = connection.job_query_results job_id, token: token if resp.success? QueryData.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#next? ⇒ Boolean
Whether there is a next page of query data.
92 93 94 |
# File 'lib/gcloud/bigquery/query_data.rb', line 92 def next? !token.nil? end |
#schema ⇒ Object
The schema of the data.
53 54 55 56 57 58 |
# File 'lib/gcloud/bigquery/query_data.rb', line 53 def schema s = @gapi["schema"] s = s.to_hash if s.respond_to? :to_hash s = {} if s.nil? s end |
#total_bytes ⇒ Object
The total number of bytes processed for this query.
36 37 38 |
# File 'lib/gcloud/bigquery/query_data.rb', line 36 def total_bytes @gapi["totalBytesProcessed"] end |