Class: Gcloud::Bigquery::Table::Schema
- Inherits:
-
Object
- Object
- Gcloud::Bigquery::Table::Schema
- 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
-
#fields ⇒ Object
readonly
:nodoc:.
Instance Method Summary collapse
-
#boolean(name, description: nil, mode: nil) ⇒ Object
Adds a boolean field to the schema.
-
#changed? ⇒ Boolean
:nodoc:.
-
#float(name, description: nil, mode: nil) ⇒ Object
Adds a floating-point number field to the schema.
-
#initialize(schema = nil, nested = false) ⇒ Schema
constructor
Initializes a new schema object with an existing schema.
-
#integer(name, description: nil, mode: nil) ⇒ Object
Adds an integer field to the schema.
-
#record(name, description: nil, mode: nil) {|nested_schema| ... } ⇒ Object
Adds a record field to the schema.
-
#schema ⇒ Object
Returns the schema as hash containing the keys and values specified by the Google Cloud BigQuery Rest API .
-
#string(name, description: nil, mode: nil) ⇒ Object
Adds a string field to the schema.
-
#timestamp(name, description: nil, mode: nil) ⇒ Object
Adds a timestamp field to the schema.
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
#fields ⇒ Object (readonly)
:nodoc:
47 48 49 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 47 def fields @fields end |
Instance Method Details
#boolean(name, description: nil, mode: nil) ⇒ 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) description-
A description of the field. (
String) mode-
The field’s mode. The possible values are
:nullable,:required, and:repeated. The default value is:nullable. (Symbol)
141 142 143 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 141 def boolean name, description: nil, mode: nil add_field name, :boolean, nil, description: description, mode: mode end |
#changed? ⇒ Boolean
:nodoc:
58 59 60 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 58 def changed? #:nodoc: @original_fields != @fields end |
#float(name, description: nil, mode: nil) ⇒ 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) description-
A description of the field. (
String) mode-
The field’s mode. The possible values are
:nullable,:required, and:repeated. The default value is:nullable. (Symbol)
123 124 125 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 123 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-
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) description-
A description of the field. (
String) mode-
The field’s mode. The possible values are
:nullable,:required, and:repeated. The default value is:nullable. (Symbol)
105 106 107 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 105 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 .
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) description-
A description of the field. (
String) 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
198 199 200 201 202 203 204 205 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 198 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 |
#schema ⇒ Object
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, description: nil, mode: nil) ⇒ 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) description-
A description of the field. (
String) mode-
The field’s mode. The possible values are
:nullable,:required, and:repeated. The default value is:nullable. (Symbol)
87 88 89 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 87 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-
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) description-
A description of the field. (
String) mode-
The field’s mode. The possible values are
:nullable,:required, and:repeated. The default value is:nullable. (Symbol)
159 160 161 |
# File 'lib/gcloud/bigquery/table/schema.rb', line 159 def name, description: nil, mode: nil add_field name, :timestamp, nil, description: description, mode: mode end |