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 Table#schema. Supports nested and repeated fields via a nested block. For more information about BigQuery schema definitions, see Preparing Data for BigQuery .

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

Constant Summary collapse

MODES =

:nodoc:

%w( NULLABLE REQUIRED REPEATED )
TYPES =

:nodoc:

%w( STRING INTEGER FLOAT BOOLEAN TIMESTAMP RECORD )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes a new schema object with an existing schema.



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

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

Instance Attribute Details

#fieldsObject (readonly)

:nodoc:



47
48
49
# File 'lib/gcloud/bigquery/table/schema.rb', line 47

def fields
  @fields
end

Instance Method Details

#boolean(name, options = {}) ⇒ Object

Adds a boolean field to the schema.

Parameters

name

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. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:description]

A description of the field. (String)

options[:mode]

The field’s mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable. (Symbol)



149
150
151
# File 'lib/gcloud/bigquery/table/schema.rb', line 149

def boolean name, options = {}
  add_field name, :boolean, nil, options
end

#changed?Boolean

:nodoc:

Returns:

  • (Boolean)


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

def changed? #:nodoc:
  @original_fields != @fields
end

#float(name, options = {}) ⇒ Object

Adds a floating-point number field to the schema.

Parameters

name

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. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:description]

A description of the field. (String)

options[:mode]

The field’s mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable. (Symbol)



129
130
131
# File 'lib/gcloud/bigquery/table/schema.rb', line 129

def float name, options = {}
  add_field name, :float, nil, options
end

#integer(name, options = {}) ⇒ Object

Adds an integer field to the schema.

Parameters

name

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. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:description]

A description of the field. (String)

options[:mode]

The field’s mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable. (Symbol)



109
110
111
# File 'lib/gcloud/bigquery/table/schema.rb', line 109

def integer name, options = {}
  add_field name, :integer, nil, options
end

#record(name, options = {}) {|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 .

Parameters

name

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. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:description]

A description of the field. (String)

options[:mode]

The field’s mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable. (Symbol)

Example

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

Yields:

  • (nested_schema)


210
211
212
213
214
215
216
# File 'lib/gcloud/bigquery/table/schema.rb', line 210

def record name, options = {}
  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, options
end

#schemaObject

Returns the schema as hash containing the keys and values specified by the Google Cloud BigQuery Rest API .



67
68
69
70
71
# File 'lib/gcloud/bigquery/table/schema.rb', line 67

def schema #:nodoc:
  {
    "fields" => @fields
  }
end

#string(name, options = {}) ⇒ Object

Adds a string field to the schema.

Parameters

name

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. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:description]

A description of the field. (String)

options[:mode]

The field’s mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable. (Symbol)



89
90
91
# File 'lib/gcloud/bigquery/table/schema.rb', line 89

def string name, options = {}
  add_field name, :string, nil, options
end

#timestamp(name, options = {}) ⇒ Object

Adds a timestamp field to the schema.

Parameters

name

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. (String)

options

An optional Hash for controlling additional behavior. (Hash)

options[:description]

A description of the field. (String)

options[:mode]

The field’s mode. The possible values are :nullable, :required, and :repeated. The default value is :nullable. (Symbol)



169
170
171
# File 'lib/gcloud/bigquery/table/schema.rb', line 169

def timestamp name, options = {}
  add_field name, :timestamp, nil, options
end