Class: Google::Cloud::Bigquery::Schema
- Inherits:
-
Object
- Object
- Google::Cloud::Bigquery::Schema
- Defined in:
- lib/google/cloud/bigquery/schema.rb,
lib/google/cloud/bigquery/schema/field.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.
Defined Under Namespace
Classes: Field
Class Method Summary collapse
-
.dump(schema, destination) ⇒ Schema
Write a schema as JSON to a file.
-
.load(source) ⇒ Schema
Load a schema from a JSON file.
Instance Method Summary collapse
-
#boolean(name, description: nil, mode: :nullable) ⇒ Object
Adds a boolean field to the schema.
-
#bytes(name, description: nil, mode: :nullable) ⇒ Object
Adds a bytes field to the schema.
-
#date(name, description: nil, mode: :nullable) ⇒ Object
Adds a date field to the schema.
-
#datetime(name, description: nil, mode: :nullable) ⇒ Object
Adds a datetime field to the schema.
-
#dump(destination) ⇒ Schema
Write the schema as JSON to a file.
-
#empty? ⇒ Boolean
Whether the schema has no fields defined.
-
#field(name) {|f| ... } ⇒ Field
Retrieve a field by name.
-
#fields ⇒ Array<Field>
The fields of the table schema.
-
#float(name, description: nil, mode: :nullable) ⇒ Object
Adds a floating-point number field to the schema.
-
#headers ⇒ Array<Symbol>
The names of the fields as symbols.
-
#integer(name, description: nil, mode: :nullable) ⇒ Object
Adds an integer field to the schema.
-
#load(source) ⇒ Schema
Load the schema from a JSON file.
-
#numeric(name, description: nil, mode: :nullable) ⇒ Object
Adds a numeric number field to the schema.
-
#param_types ⇒ Hash
The types of the fields, using the same format as the optional query parameter types.
-
#record(name, description: nil, mode: nil) {|field| ... } ⇒ Object
Adds a record field to the schema.
-
#string(name, description: nil, mode: :nullable) ⇒ Object
Adds a string field to the schema.
-
#time(name, description: nil, mode: :nullable) ⇒ Object
Adds a time field to the schema.
-
#timestamp(name, description: nil, mode: :nullable) ⇒ Object
Adds a timestamp field to the schema.
Class Method Details
.dump(schema, destination) ⇒ Schema
Write a schema as JSON to a file.
The JSON schema file is the same as for the bq
CLI.
105 106 107 |
# File 'lib/google/cloud/bigquery/schema.rb', line 105 def dump schema, destination schema.dump destination end |
.load(source) ⇒ Schema
Load a schema from a JSON file.
The JSON schema file is the same as for the bq
CLI
consisting of an array of JSON objects containing the following:
name
: The column nametype
: The column's data typedescription
: (Optional) The column's descriptionmode
: (Optional) The column's mode (if unspecified, mode defaults toNULLABLE
)fields
: Iftype
isRECORD
, an array of objects defining child fields with these properties
77 78 79 |
# File 'lib/google/cloud/bigquery/schema.rb', line 77 def load source new.load source end |
Instance Method Details
#boolean(name, description: nil, mode: :nullable) ⇒ Object
Adds a boolean field to the schema.
364 365 366 |
# File 'lib/google/cloud/bigquery/schema.rb', line 364 def boolean name, description: nil, mode: :nullable add_field name, :boolean, description: description, mode: mode end |
#bytes(name, description: nil, mode: :nullable) ⇒ Object
Adds a bytes field to the schema.
380 381 382 |
# File 'lib/google/cloud/bigquery/schema.rb', line 380 def bytes name, description: nil, mode: :nullable add_field name, :bytes, description: description, mode: mode end |
#date(name, description: nil, mode: :nullable) ⇒ Object
Adds a date field to the schema.
443 444 445 |
# File 'lib/google/cloud/bigquery/schema.rb', line 443 def date name, description: nil, mode: :nullable add_field name, :date, description: description, mode: mode end |
#datetime(name, description: nil, mode: :nullable) ⇒ Object
Adds a datetime field to the schema.
427 428 429 |
# File 'lib/google/cloud/bigquery/schema.rb', line 427 def datetime name, description: nil, mode: :nullable add_field name, :datetime, description: description, mode: mode end |
#dump(destination) ⇒ Schema
Write the schema as JSON to a file.
The JSON schema file is the same as for the bq
CLI.
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/google/cloud/bigquery/schema.rb', line 275 def dump destination if destination.respond_to?(:rewind) && destination.respond_to?(:write) destination.rewind destination.write JSON.dump(fields.map(&:to_hash)) else File.write String(destination), JSON.dump(fields.map(&:to_hash)) end self end |
#empty? ⇒ Boolean
Whether the schema has no fields defined.
205 206 207 |
# File 'lib/google/cloud/bigquery/schema.rb', line 205 def empty? fields.empty? end |
#field(name) {|f| ... } ⇒ Field
Retrieve a field by name.
193 194 195 196 197 198 |
# File 'lib/google/cloud/bigquery/schema.rb', line 193 def field name f = fields.find { |fld| fld.name == name.to_s } return nil if f.nil? yield f if block_given? f end |
#fields ⇒ Array<Field>
The fields of the table schema.
127 128 129 130 131 132 133 |
# File 'lib/google/cloud/bigquery/schema.rb', line 127 def fields if frozen? Array(@gapi.fields).map { |f| Field.from_gapi(f).freeze }.freeze else Array(@gapi.fields).map { |f| Field.from_gapi f } end end |
#float(name, description: nil, mode: :nullable) ⇒ Object
Adds a floating-point number field to the schema.
330 331 332 |
# File 'lib/google/cloud/bigquery/schema.rb', line 330 def float name, description: nil, mode: :nullable add_field name, :float, description: description, mode: mode end |
#headers ⇒ Array<Symbol>
The names of the fields as symbols.
153 154 155 |
# File 'lib/google/cloud/bigquery/schema.rb', line 153 def headers fields.map(&:name).map(&:to_sym) end |
#integer(name, description: nil, mode: :nullable) ⇒ Object
Adds an integer field to the schema.
314 315 316 |
# File 'lib/google/cloud/bigquery/schema.rb', line 314 def integer name, description: nil, mode: :nullable add_field name, :integer, description: description, mode: mode end |
#load(source) ⇒ Schema
Load the schema from a JSON file.
The JSON schema file is the same as for the bq
CLI
consisting of an array of JSON objects containing the following:
name
: The column nametype
: The column's data typedescription
: (Optional) The column's descriptionmode
: (Optional) The column's mode (if unspecified, mode defaults toNULLABLE
)fields
: Iftype
isRECORD
, an array of objects defining child fields with these properties
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/google/cloud/bigquery/schema.rb', line 239 def load source if source.respond_to?(:rewind) && source.respond_to?(:read) source.rewind schema_json = String source.read elsif source.is_a? Array schema_json = JSON.dump source else schema_json = String source end schema_json = %({"fields":#{schema_json}}) @gapi = Google::Apis::BigqueryV2::TableSchema.from_json schema_json self end |
#numeric(name, description: nil, mode: :nullable) ⇒ Object
Adds a numeric number field to the schema. Numeric is a fixed-precision numeric type with 38 decimal digits, 9 that follow the decimal point.
348 349 350 |
# File 'lib/google/cloud/bigquery/schema.rb', line 348 def numeric name, description: nil, mode: :nullable add_field name, :numeric, description: description, mode: mode end |
#param_types ⇒ Hash
The types of the fields, using the same format as the optional query parameter types.
174 175 176 |
# File 'lib/google/cloud/bigquery/schema.rb', line 174 def param_types Hash[fields.map { |field| [field.name.to_sym, field.param_type] }] end |
#record(name, description: nil, mode: nil) {|field| ... } ⇒ 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 Loading denormalized, nested, and repeated data .
481 482 483 484 485 486 487 488 |
# File 'lib/google/cloud/bigquery/schema.rb', line 481 def record name, description: nil, mode: nil # TODO: do we need to raise if no block was given? raise ArgumentError, "a block is required" unless block_given? nested_field = add_field name, :record, description: description, mode: mode yield nested_field nested_field end |
#string(name, description: nil, mode: :nullable) ⇒ Object
Adds a string field to the schema.
298 299 300 |
# File 'lib/google/cloud/bigquery/schema.rb', line 298 def string name, description: nil, mode: :nullable add_field name, :string, description: description, mode: mode end |
#time(name, description: nil, mode: :nullable) ⇒ Object
Adds a time field to the schema.
411 412 413 |
# File 'lib/google/cloud/bigquery/schema.rb', line 411 def time name, description: nil, mode: :nullable add_field name, :time, description: description, mode: mode end |
#timestamp(name, description: nil, mode: :nullable) ⇒ Object
Adds a timestamp field to the schema.
395 396 397 |
# File 'lib/google/cloud/bigquery/schema.rb', line 395 def name, description: nil, mode: :nullable add_field name, :timestamp, description: description, mode: mode end |