Class: Gcloud::Bigquery::Data

Inherits:
Array
  • Object
show all
Defined in:
lib/gcloud/bigquery/data.rb

Overview

# Data

Represents Table Data as a list of name/value pairs. Also contains metadata such as ‘etag` and `total`.

Direct Known Subclasses

QueryData

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arr = []) ⇒ Data

Returns a new instance of Data.



36
37
38
39
40
# File 'lib/gcloud/bigquery/data.rb', line 36

def initialize arr = []
  @table = nil
  @gapi = {}
  super arr
end

Instance Attribute Details

#gapiObject



33
34
35
# File 'lib/gcloud/bigquery/data.rb', line 33

def gapi
  @gapi
end

#tableObject



29
30
31
# File 'lib/gcloud/bigquery/data.rb', line 29

def table
  @table
end

Class Method Details

.format_rows(rows, fields) ⇒ Object

rubocop:disable all Disabled rubocop because this implementation will not last.



198
199
200
201
202
203
204
205
206
207
# File 'lib/gcloud/bigquery/data.rb', line 198

def self.format_rows rows, fields
  headers = Array(fields).map { |f| f.name }
  field_types = Array(fields).map { |f| f.type }

  Array(rows).map do |row|
    values = row.f.map { |f| f.v }
    formatted_values = format_values field_types, values
    Hash[headers.zip formatted_values]
  end
end

.format_values(field_types, values) ⇒ Object



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/gcloud/bigquery/data.rb', line 209

def self.format_values field_types, values
  field_types.zip(values).map do |type, value|
    begin
      if value.nil?
        nil
      elsif type == "INTEGER"
        Integer value
      elsif type == "FLOAT"
        Float value
      elsif type == "BOOLEAN"
        (value == "true" ? true : (value == "false" ? false : nil))
      else
        value
      end
    rescue
      value
    end
  end
end

.from_gapi(gapi, table) ⇒ Object



186
187
188
189
190
191
192
193
# File 'lib/gcloud/bigquery/data.rb', line 186

def self.from_gapi gapi, table
  formatted_rows = format_rows gapi.rows, table.fields

  data = new formatted_rows
  data.table = table
  data.gapi = gapi
  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.

Examples:

Iterating each rows by passing a block:

require "gcloud"

gcloud = Gcloud.new
bigquery = gcloud.bigquery
table = dataset.table "my_table"

table.data.all do |row|
  puts row["word"]
end

Using the enumerator by not passing a block:

require "gcloud"

gcloud = Gcloud.new
bigquery = gcloud.bigquery
table = dataset.table "my_table"

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

Limit the number of API calls made:

require "gcloud"

gcloud = Gcloud.new
bigquery = gcloud.bigquery
table = dataset.table "my_table"

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

Parameters:

  • request_limit (Integer) (defaults to: nil)

    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)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/gcloud/bigquery/data.rb', line 162

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

#etagObject

The etag.



50
51
52
# File 'lib/gcloud/bigquery/data.rb', line 50

def etag
  @gapi.etag
end

#kindObject

The resource type of the API response.



44
45
46
# File 'lib/gcloud/bigquery/data.rb', line 44

def kind
  @gapi.kind
end

#nextData

Retrieve the next page of data.

Examples:

require "gcloud"

gcloud = Gcloud.new
bigquery = gcloud.bigquery
table = dataset.table "my_table"

data = table.data
if data.next?
  next_data = data.next
end

Returns:



105
106
107
108
109
# File 'lib/gcloud/bigquery/data.rb', line 105

def next
  return nil unless next?
  ensure_table!
  table.data token: token
end

#next?Boolean

Whether there is a next page of data.

Examples:

require "gcloud"

gcloud = Gcloud.new
bigquery = gcloud.bigquery
table = dataset.table "my_table"

data = table.data
if data.next?
  next_data = data.next
end

Returns:

  • (Boolean)


84
85
86
# File 'lib/gcloud/bigquery/data.rb', line 84

def next?
  !token.nil?
end

#rawObject

Represents Table Data as a list of positional values (array of arrays). No type conversion is made, e.g. numbers are formatted as strings.



180
181
182
# File 'lib/gcloud/bigquery/data.rb', line 180

def raw
  Array(gapi.rows).map { |row| row.f.map(&:v) }
end

#tokenObject

A token used for paging results.



56
57
58
# File 'lib/gcloud/bigquery/data.rb', line 56

def token
  @gapi.page_token
end

#totalObject

The total number of rows in the complete table.



61
62
63
64
65
# File 'lib/gcloud/bigquery/data.rb', line 61

def total
  Integer @gapi.total_rows
rescue
  nil
end