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.



773
774
775
776
# File 'lib/fluent/plugin/out_bigquery.rb', line 773

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

Instance Method Details

#[](name) ⇒ Object



782
783
784
# File 'lib/fluent/plugin/out_bigquery.rb', line 782

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

#empty?Boolean

Returns:

  • (Boolean)


786
787
788
# File 'lib/fluent/plugin/out_bigquery.rb', line 786

def empty?
  @fields.empty?
end

#format_one(record) ⇒ Object



843
844
845
846
847
848
849
850
851
# File 'lib/fluent/plugin/out_bigquery.rb', line 843

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



805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# File 'lib/fluent/plugin/out_bigquery.rb', line 805

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



827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
# File 'lib/fluent/plugin/out_bigquery.rb', line 827

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



790
791
792
793
794
# File 'lib/fluent/plugin/out_bigquery.rb', line 790

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

#to_hObject



796
797
798
799
800
801
802
803
# File 'lib/fluent/plugin/out_bigquery.rb', line 796

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

#typeObject



778
779
780
# File 'lib/fluent/plugin/out_bigquery.rb', line 778

def type
  :record
end