Class: Gcloud::Bigquery::Schema

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

Overview

# Table Schema

A builder for BigQuery table schemas, passed to block arguments to Dataset#create_table and Table#schema. Supports nested and repeated fields via a nested block.

Examples:

require "gcloud"

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

table.schema do |schema|
  schema.string "first_name", mode: :required
  schema.record "cities_lived", mode: :repeated do |cities_lived|
    cities_lived.string "place", mode: :required
    cities_lived.integer "number_of_years", mode: :required
  end
end

See Also:

Defined Under Namespace

Classes: Field

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSchema

Returns a new instance of Schema.



45
46
47
# File 'lib/gcloud/bigquery/schema.rb', line 45

def initialize
  @nested = nil
end

Class Method Details

.from_gapi(gapi) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/gcloud/bigquery/schema.rb', line 89

def self.from_gapi gapi
  gapi ||= Google::Apis::BigqueryV2::TableSchema.new fields: []
  gapi.fields ||= []
  new.tap do |s|
    s.instance_variable_set :@gapi, gapi
    s.instance_variable_set :@original_json, gapi.to_json
  end
end

Instance Method Details

#==(other) ⇒ Object



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

def == other
  return false unless other.is_a? Schema
  to_gapi.to_h == other.to_gapi.to_h
end

#boolean(name, description: nil, mode: :nullable) ⇒ Object

Adds a boolean field to the schema.

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field’s mode. The possible values are ‘:nullable`, `:required`, and `:repeated`. The default value is `:nullable`.



166
167
168
# File 'lib/gcloud/bigquery/schema.rb', line 166

def boolean name, description: nil, mode: :nullable
  add_field name, :boolean, nil, description: description, mode: mode
end

#changed?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/gcloud/bigquery/schema.rb', line 63

def changed?
  return false if frozen?
  check_for_mutated_schema!
  @original_json != @gapi.to_json
end

#check_for_mutated_schema!Object



80
81
82
83
84
85
86
# File 'lib/gcloud/bigquery/schema.rb', line 80

def check_for_mutated_schema!
  return if frozen?
  return if @gapi.frozen?
  return if @fields.nil?
  gapi_fields = Array(@fields).map(&:to_gapi)
  @gapi.update! fields: gapi_fields
end

#empty?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/gcloud/bigquery/schema.rb', line 58

def empty?
  fields.empty?
end

#fieldsObject



49
50
51
# File 'lib/gcloud/bigquery/schema.rb', line 49

def fields
  @fields ||= @gapi.fields.map { |f| Field.from_gapi f }
end

#fields=(new_fields) ⇒ Object



53
54
55
56
# File 'lib/gcloud/bigquery/schema.rb', line 53

def fields= new_fields
  @gapi.fields = Array(new_fields).map(&:to_gapi)
  @fields = @gapi.fields.map { |f| Field.from_gapi f }
end

#float(name, description: nil, mode: :nullable) ⇒ Object

Adds a floating-point number field to the schema.

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field’s mode. The possible values are ‘:nullable`, `:required`, and `:repeated`. The default value is `:nullable`.



151
152
153
# File 'lib/gcloud/bigquery/schema.rb', line 151

def float name, description: nil, mode: :nullable
  add_field name, :float, nil, description: description, mode: mode
end

#freezeObject



70
71
72
73
74
75
76
# File 'lib/gcloud/bigquery/schema.rb', line 70

def freeze
  @gapi = @gapi.dup.freeze
  @gapi.fields.freeze
  @fields = @gapi.fields.map { |f| Field.from_gapi(f).freeze }
  @fields.freeze
  super
end

#integer(name, description: nil, mode: :nullable) ⇒ Object

Adds an integer field to the schema.

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field’s mode. The possible values are ‘:nullable`, `:required`, and `:repeated`. The default value is `:nullable`.



136
137
138
# File 'lib/gcloud/bigquery/schema.rb', line 136

def integer name, description: nil, mode: :nullable
  add_field name, :integer, nil, description: description, mode: mode
end

#record(name, description: nil, mode: nil) {|nested_schema| ... } ⇒ Object

Adds a record field to the schema. A block must be passed describing the nested fields of the record. For more information about nested and repeated records, see [Preparing Data for BigQuery ](cloud.google.com/bigquery/preparing-data-for-bigquery).

Examples:

require "gcloud"

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

table.schema do |schema|
  schema.string "first_name", mode: :required
  schema.record "cities_lived", mode: :repeated do |cities_lived|
    cities_lived.string "place", mode: :required
    cities_lived.integer "number_of_years", mode: :required
  end
end

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: nil)

    The field’s mode. The possible values are ‘:nullable`, `:required`, and `:repeated`. The default value is `:nullable`.

Yields:

  • (nested_schema)

    a block for setting the nested schema

Yield Parameters:

  • nested_schema (Table::Schema)

    the object accepting the nested schema



219
220
221
222
223
224
225
226
227
228
229
# File 'lib/gcloud/bigquery/schema.rb', line 219

def record name, description: nil, mode: nil
  fail ArgumentError, "nested RECORD type is not permitted" if @nested
  fail ArgumentError, "a block is required" unless block_given?
  empty_schema = Google::Apis::BigqueryV2::TableSchema.new fields: []
  nested_schema = self.class.from_gapi(empty_schema).tap do |s|
    s.instance_variable_set :@nested, true
  end
  yield nested_schema
  add_field name, :record, nested_schema.fields,
            description: description, mode: mode
end

#string(name, description: nil, mode: :nullable) ⇒ Object

Adds a string field to the schema.

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field’s mode. The possible values are ‘:nullable`, `:required`, and `:repeated`. The default value is `:nullable`.



121
122
123
# File 'lib/gcloud/bigquery/schema.rb', line 121

def string name, description: nil, mode: :nullable
  add_field name, :string, nil, description: description, mode: mode
end

#timestamp(name, description: nil, mode: :nullable) ⇒ Object

Adds a timestamp field to the schema.

Parameters:

  • name (String)

    The field name. The name must contain only letters (a-z, A-Z), numbers (0-9), or underscores (_), and must start with a letter or underscore. The maximum length is 128 characters.

  • description (String) (defaults to: nil)

    A description of the field.

  • mode (Symbol) (defaults to: :nullable)

    The field’s mode. The possible values are ‘:nullable`, `:required`, and `:repeated`. The default value is `:nullable`.



181
182
183
# File 'lib/gcloud/bigquery/schema.rb', line 181

def timestamp name, description: nil, mode: :nullable
  add_field name, :timestamp, nil, description: description, mode: mode
end

#to_gapiObject



99
100
101
102
# File 'lib/gcloud/bigquery/schema.rb', line 99

def to_gapi
  check_for_mutated_schema!
  @gapi
end