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

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/bigquery/schema/field.rb

Overview

Schema Field

The fields of a table schema.

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

See Also:

Instance Method Summary collapse

Instance Method Details

#boolean(name, description: nil, mode: :nullable) ⇒ Object

Adds a boolean field to the schema.

This can only be called on fields that are of type RECORD.

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.



277
278
279
280
281
# File 'lib/google/cloud/bigquery/schema/field.rb', line 277

def boolean name, description: nil, mode: :nullable
  record_check!

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

#boolean?Boolean

Checks if the mode of the field is BOOLEAN.

Returns:

  • (Boolean)


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

def boolean?
  mode == "BOOLEAN"
end

#bytes(name, description: nil, mode: :nullable) ⇒ Object

Adds a bytes field to the schema.

This can only be called on fields that are of type RECORD.

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.



296
297
298
299
300
# File 'lib/google/cloud/bigquery/schema/field.rb', line 296

def bytes name, description: nil, mode: :nullable
  record_check!

  add_field name, :bytes, description: description, mode: mode
end

#bytes?Boolean

Checks if the mode of the field is BYTES.

Returns:

  • (Boolean)


145
146
147
# File 'lib/google/cloud/bigquery/schema/field.rb', line 145

def bytes?
  mode == "BYTES"
end

#date(name, description: nil, mode: :nullable) ⇒ Object

Adds a date field to the schema.

This can only be called on fields that are of type RECORD.

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.



372
373
374
375
376
# File 'lib/google/cloud/bigquery/schema/field.rb', line 372

def date name, description: nil, mode: :nullable
  record_check!

  add_field name, :date, description: description, mode: mode
end

#date?Boolean

Checks if the mode of the field is DATE.

Returns:

  • (Boolean)


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

def date?
  mode == "DATE"
end

#datetime(name, description: nil, mode: :nullable) ⇒ Object

Adds a datetime field to the schema.

This can only be called on fields that are of type RECORD.

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.



353
354
355
356
357
# File 'lib/google/cloud/bigquery/schema/field.rb', line 353

def datetime name, description: nil, mode: :nullable
  record_check!

  add_field name, :datetime, description: description, mode: mode
end

#datetime?Boolean

Checks if the mode of the field is DATETIME.

Returns:

  • (Boolean)


163
164
165
# File 'lib/google/cloud/bigquery/schema/field.rb', line 163

def datetime?
  mode == "DATETIME"
end

#descriptionObject

The description of the field.



94
95
96
# File 'lib/google/cloud/bigquery/schema/field.rb', line 94

def description
  @gapi.description
end

#description=(new_description) ⇒ Object

Updates the description of the field.



101
102
103
# File 'lib/google/cloud/bigquery/schema/field.rb', line 101

def description= new_description
  @gapi.update! description: new_description
end

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

Retreive a nested fields by name, if the type property is set to RECORD. Will return nil otherwise.

Yields:

  • (f)


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

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

#fieldsObject

The nested fields if the type property is set to RECORD. Will be empty otherwise.



182
183
184
185
186
187
188
# File 'lib/google/cloud/bigquery/schema/field.rb', line 182

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.

This can only be called on fields that are of type RECORD.

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.



258
259
260
261
262
# File 'lib/google/cloud/bigquery/schema/field.rb', line 258

def float name, description: nil, mode: :nullable
  record_check!

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

#float?Boolean

Checks if the mode of the field is FLOAT.

Returns:

  • (Boolean)


133
134
135
# File 'lib/google/cloud/bigquery/schema/field.rb', line 133

def float?
  mode == "FLOAT"
end

#headersObject

The names of the nested fields as symbols if the type property is set to RECORD. Will be empty otherwise.



193
194
195
# File 'lib/google/cloud/bigquery/schema/field.rb', line 193

def headers
  fields.map(&:name).map(&:to_sym)
end

#integer(name, description: nil, mode: :nullable) ⇒ Object

Adds an integer field to the schema.

This can only be called on fields that are of type RECORD.

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.



239
240
241
242
243
# File 'lib/google/cloud/bigquery/schema/field.rb', line 239

def integer name, description: nil, mode: :nullable
  record_check!

  add_field name, :integer, description: description, mode: mode
end

#integer?Boolean

Checks if the mode of the field is INTEGER.

Returns:

  • (Boolean)


127
128
129
# File 'lib/google/cloud/bigquery/schema/field.rb', line 127

def integer?
  mode == "INTEGER"
end

#modeObject

The mode of the field.



108
109
110
# File 'lib/google/cloud/bigquery/schema/field.rb', line 108

def mode
  @gapi.mode
end

#mode=(new_mode) ⇒ Object

Updates the mode of the field.



115
116
117
# File 'lib/google/cloud/bigquery/schema/field.rb', line 115

def mode= new_mode
  @gapi.update! mode: verify_mode(new_mode)
end

#nameObject

The name of the field.



48
49
50
# File 'lib/google/cloud/bigquery/schema/field.rb', line 48

def name
  @gapi.name
end

#name=(new_name) ⇒ Object

Updates the name of the field.



55
56
57
# File 'lib/google/cloud/bigquery/schema/field.rb', line 55

def name= new_name
  @gapi.update! name: String(new_name)
end

#nullable?Boolean

Checks if the type of the field is NULLABLE.

Returns:

  • (Boolean)


75
76
77
# File 'lib/google/cloud/bigquery/schema/field.rb', line 75

def nullable?
  mode == "NULLABLE"
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 .

This can only be called on fields that are of type RECORD.

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:

  • (nested_schema)

    a block for setting the nested schema

Yield Parameters:

  • nested_schema (Schema)

    the object accepting the nested schema



413
414
415
416
417
418
419
420
421
422
423
# File 'lib/google/cloud/bigquery/schema/field.rb', line 413

def record name, description: nil, mode: nil
  record_check!

  # 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

#record?Boolean

Checks if the mode of the field is RECORD.

Returns:

  • (Boolean)


175
176
177
# File 'lib/google/cloud/bigquery/schema/field.rb', line 175

def record?
  mode == "RECORD"
end

#repeated?Boolean

Checks if the type of the field is REPEATED.

Returns:

  • (Boolean)


87
88
89
# File 'lib/google/cloud/bigquery/schema/field.rb', line 87

def repeated?
  mode == "REPEATED"
end

#required?Boolean

Checks if the type of the field is REQUIRED.

Returns:

  • (Boolean)


81
82
83
# File 'lib/google/cloud/bigquery/schema/field.rb', line 81

def required?
  mode == "REQUIRED"
end

#string(name, description: nil, mode: :nullable) ⇒ Object

Adds a string field to the schema.

This can only be called on fields that are of type RECORD.

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.



220
221
222
223
224
# File 'lib/google/cloud/bigquery/schema/field.rb', line 220

def string name, description: nil, mode: :nullable
  record_check!

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

#string?Boolean

Checks if the mode of the field is STRING.

Returns:

  • (Boolean)


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

def string?
  mode == "STRING"
end

#time(name, description: nil, mode: :nullable) ⇒ Object

Adds a time field to the schema.

This can only be called on fields that are of type RECORD.

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.



334
335
336
337
338
# File 'lib/google/cloud/bigquery/schema/field.rb', line 334

def time name, description: nil, mode: :nullable
  record_check!

  add_field name, :time, description: description, mode: mode
end

#time?Boolean

Checks if the mode of the field is TIME.

Returns:

  • (Boolean)


157
158
159
# File 'lib/google/cloud/bigquery/schema/field.rb', line 157

def time?
  mode == "TIME"
end

#timestamp(name, description: nil, mode: :nullable) ⇒ Object

Adds a timestamp field to the schema.

This can only be called on fields that are of type RECORD.

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.



315
316
317
318
319
# File 'lib/google/cloud/bigquery/schema/field.rb', line 315

def timestamp name, description: nil, mode: :nullable
  record_check!

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

#timestamp?Boolean

Checks if the mode of the field is TIMESTAMP.

Returns:

  • (Boolean)


151
152
153
# File 'lib/google/cloud/bigquery/schema/field.rb', line 151

def timestamp?
  mode == "TIMESTAMP"
end

#typeObject

The type of the field.



62
63
64
# File 'lib/google/cloud/bigquery/schema/field.rb', line 62

def type
  @gapi.type
end

#type=(new_type) ⇒ Object

Updates the type of the field.



69
70
71
# File 'lib/google/cloud/bigquery/schema/field.rb', line 69

def type= new_type
  @gapi.update! type: verify_type(new_type)
end