Class: Gcloud::Bigquery::Table::Schema

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

Overview

# Table Schema

A builder for BigQuery table schemas, passed to block arguments to Dataset#create_table and #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:

Constant Summary collapse

MODES =
%w( NULLABLE REQUIRED REPEATED )
TYPES =
%w( STRING INTEGER FLOAT BOOLEAN TIMESTAMP RECORD )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema = nil, nested = false) ⇒ Schema

Returns a new instance of Schema.



57
58
59
60
61
62
# File 'lib/gcloud/bigquery/table/schema.rb', line 57

def initialize schema = nil, nested = false
  fields = (schema && schema["fields"]) || []
  @original_fields = fields.dup
  @fields = fields.dup
  @nested = nested
end

Instance Attribute Details

#fieldsObject (readonly)



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

def fields
  @fields
end

Instance Method Details

#boolean(name, description: nil, mode: nil) ⇒ 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: nil)

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



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

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

#changed?Boolean

Returns:

  • (Boolean)


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

def changed?
  @original_fields != @fields
end

#float(name, description: nil, mode: nil) ⇒ 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: nil)

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



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

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

#integer(name, description: nil, mode: nil) ⇒ 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: nil)

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



107
108
109
# File 'lib/gcloud/bigquery/table/schema.rb', line 107

def integer name, description: nil, mode: nil
  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



190
191
192
193
194
195
196
197
# File 'lib/gcloud/bigquery/table/schema.rb', line 190

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?
  nested_schema = self.class.new nil, true
  yield nested_schema
  add_field name, :record, nested_schema.fields,
            description: description, mode: mode
end

#schemaObject

Returns the schema as hash containing the keys and values specified by the Google Cloud BigQuery [Rest API ](cloud.google.com/bigquery/docs/reference/v2/tables#resource) .



75
76
77
78
79
# File 'lib/gcloud/bigquery/table/schema.rb', line 75

def schema
  {
    "fields" => @fields
  }
end

#string(name, description: nil, mode: nil) ⇒ 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: nil)

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



92
93
94
# File 'lib/gcloud/bigquery/table/schema.rb', line 92

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

#timestamp(name, description: nil, mode: nil) ⇒ 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: nil)

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



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

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