Class: Fluent::BigQueryOutput::FieldSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/out_bigquery.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, mode = :nullable) ⇒ FieldSchema

Returns a new instance of FieldSchema.



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/fluent/plugin/out_bigquery.rb', line 426

def initialize(name, mode = :nullable)
  unless [:nullable, :required, :repeated].include?(mode)
    raise ConfigError, "Unrecognized mode for #{name}: #{mode}"
  end
  ### https://developers.google.com/bigquery/docs/tables
  # Each field has the following properties:
  #
  # 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.
  #        https://cloud.google.com/bigquery/docs/reference/v2/tables#schema.fields.name
  unless name =~ /^[_A-Za-z][_A-Za-z0-9]{,127}$/
    raise Fluent::ConfigError, "invalid bigquery field name: '#{name}'"
  end

  @name = name
  @mode = mode
end

Instance Attribute Details

#mode ⇒ Object (readonly)

Returns the value of attribute mode.



444
445
446
# File 'lib/fluent/plugin/out_bigquery.rb', line 444

def mode
  @mode
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



444
445
446
# File 'lib/fluent/plugin/out_bigquery.rb', line 444

def name
  @name
end

Instance Method Details

#format(value) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
# File 'lib/fluent/plugin/out_bigquery.rb', line 446

def format(value)
  case @mode
  when :nullable
    format_one(value) unless value.nil?
  when :required
    raise "Required field #{name} cannot be null" if value.nil?
    format_one(value)
  when :repeated
    value.nil? ? [] : value.map {|v| format_one(v) }
  end
end

#format_one(value) ⇒ Object

Raises:

  • (NotImplementedError)


458
459
460
# File 'lib/fluent/plugin/out_bigquery.rb', line 458

def format_one(value)
  raise NotImplementedError, "Must implement in a subclass"
end

#to_h ⇒ Object



462
463
464
465
466
467
468
# File 'lib/fluent/plugin/out_bigquery.rb', line 462

def to_h
  {
    :name => name,
    :type => type.to_s.upcase,
    :mode => mode.to_s.upcase,
  }
end