Class: Google::Cloud::Bigquery::Schema

Inherits:
Object
  • Object
show all
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.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
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

Instance Method Summary collapse

Instance Method Details

#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)

    A description of the field.

  • mode (Symbol)

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



186
187
188
# File 'lib/google/cloud/bigquery/schema.rb', line 186

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.

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)

    A description of the field.

  • mode (Symbol)

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



202
203
204
# File 'lib/google/cloud/bigquery/schema.rb', line 202

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.

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)

    A description of the field.

  • mode (Symbol)

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



265
266
267
# File 'lib/google/cloud/bigquery/schema.rb', line 265

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.

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)

    A description of the field.

  • mode (Symbol)

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



249
250
251
# File 'lib/google/cloud/bigquery/schema.rb', line 249

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

#empty?Boolean

Whether the schema has no fields defined.

Returns:

  • (Boolean)

    true when there are no fields, false otherwise.



122
123
124
# File 'lib/google/cloud/bigquery/schema.rb', line 122

def empty?
  fields.empty?
end

#field(name) {|f| ... } ⇒ Field

Retrieve a field by name.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

field = table.schema.field "name"
field.required? #=> true

Yields:

  • (f)

Returns:

  • (Field)

    A field object.



110
111
112
113
114
115
# File 'lib/google/cloud/bigquery/schema.rb', line 110

def field name
  f = fields.find { |fld| fld.name == name.to_s }
  return nil if f.nil?
  yield f if block_given?
  f
end

#fieldsArray<Field>

The fields of the table schema.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.table "my_table"

schema = table.schema

schema.fields.each do |field|
  puts field.name
end

Returns:

  • (Array<Field>)

    An array of field objects.



65
66
67
68
69
70
71
# File 'lib/google/cloud/bigquery/schema.rb', line 65

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.

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)

    A description of the field.

  • mode (Symbol)

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



170
171
172
# File 'lib/google/cloud/bigquery/schema.rb', line 170

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

#headersArray<Symbol>

The names of the fields as symbols.

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
dataset = bigquery.dataset "my_dataset"
table = dataset.create_table "my_table"

schema = table.schema

schema.headers.each do |header|
  puts header
end

Returns:

  • (Array<Symbol>)

    An array of column names.



91
92
93
# File 'lib/google/cloud/bigquery/schema.rb', line 91

def headers
  fields.map(&:name).map(&:to_sym)
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)

    A description of the field.

  • mode (Symbol)

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



154
155
156
# File 'lib/google/cloud/bigquery/schema.rb', line 154

def integer name, description: nil, mode: :nullable
  add_field name, :integer, description: description, mode: mode
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 Preparing Data for BigQuery .

Examples:

require "google/cloud/bigquery"

bigquery = Google::Cloud::Bigquery.new
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)

    A description of the field.

  • mode (Symbol)

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

Yields:

  • (field)

    a block for setting the nested record's schema

Yield Parameters:

  • field (Field)

    the object accepting the nested schema



302
303
304
305
306
307
308
309
310
# File 'lib/google/cloud/bigquery/schema.rb', line 302

def record name, description: nil, mode: nil
  # TODO: do we need to fail if no block was given?
  fail 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.

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)

    A description of the field.

  • mode (Symbol)

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



138
139
140
# File 'lib/google/cloud/bigquery/schema.rb', line 138

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.

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)

    A description of the field.

  • mode (Symbol)

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



233
234
235
# File 'lib/google/cloud/bigquery/schema.rb', line 233

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.

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)

    A description of the field.

  • mode (Symbol)

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



217
218
219
# File 'lib/google/cloud/bigquery/schema.rb', line 217

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