Class: Fluent::BigQueryOutput::RecordSchema

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

Constant Summary collapse

FIELD_TYPES =
{
  string: StringFieldSchema,
  integer: IntegerFieldSchema,
  float: FloatFieldSchema,
  boolean: BooleanFieldSchema,
  timestamp: TimestampFieldSchema,
  record: RecordSchema
}.freeze

Instance Attribute Summary

Attributes inherited from FieldSchema

#mode, #name

Instance Method Summary collapse

Methods inherited from FieldSchema

#format

Constructor Details

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

Returns a new instance of RecordSchema.



737
738
739
740
# File 'lib/fluent/plugin/out_bigquery.rb', line 737

def initialize(name, mode = :nullable)
  super(name, mode)
  @fields = {}
end

Instance Method Details

#[](name) ⇒ Object



746
747
748
# File 'lib/fluent/plugin/out_bigquery.rb', line 746

def [](name)
  @fields[name]
end

#empty?Boolean

Returns:

  • (Boolean)


750
751
752
# File 'lib/fluent/plugin/out_bigquery.rb', line 750

def empty?
  @fields.empty?
end

#format_one(record) ⇒ Object



807
808
809
810
811
812
813
814
815
# File 'lib/fluent/plugin/out_bigquery.rb', line 807

def format_one(record)
  out = {}
  record.each do |key, value|
    next if value.nil?
    schema = @fields[key]
    out[key] = schema ? schema.format(value) : value
  end
  out
end

#load_schema(schema, allow_overwrite = true) ⇒ Object



769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
# File 'lib/fluent/plugin/out_bigquery.rb', line 769

def load_schema(schema, allow_overwrite=true)
  schema.each do |field|
    raise ConfigError, 'field must have type' unless field.key?('type')

    name = field['name']
    mode = (field['mode'] || 'nullable').downcase.to_sym

    type = field['type'].downcase.to_sym
    field_schema_class = FIELD_TYPES[type]
    raise ConfigError, "Invalid field type: #{field['type']}" unless field_schema_class

    next if @fields.key?(name) and !allow_overwrite

    field_schema = field_schema_class.new(name, mode)
    @fields[name] = field_schema
    if type == :record
      raise ConfigError, "record field must have fields" unless field.key?('fields')
      field_schema.load_schema(field['fields'], allow_overwrite)
    end
  end
end

#register_field(name, type) ⇒ Object



791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# File 'lib/fluent/plugin/out_bigquery.rb', line 791

def register_field(name, type)
  if @fields.key?(name) and @fields[name].type != :timestamp
    raise ConfigError, "field #{name} is registered twice"
  end
  if name[/\./]
    recordname = $`
    fieldname = $'
    register_record_field(recordname)
    @fields[recordname].register_field(fieldname, type)
  else
    schema = FIELD_TYPES[type]
    raise ConfigError, "[Bug] Invalid field type #{type}" unless schema
    @fields[name] = schema.new(name)
  end
end

#to_aObject



754
755
756
757
758
# File 'lib/fluent/plugin/out_bigquery.rb', line 754

def to_a
  @fields.map do |_, field_schema|
    field_schema.to_h
  end
end

#to_hObject



760
761
762
763
764
765
766
767
# File 'lib/fluent/plugin/out_bigquery.rb', line 760

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

#typeObject



742
743
744
# File 'lib/fluent/plugin/out_bigquery.rb', line 742

def type
  :record
end