Class: Fluent::BigQueryOutput::RecordSchema
- Inherits:
-
FieldSchema
- Object
- FieldSchema
- Fluent::BigQueryOutput::RecordSchema
- 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
Instance Method Summary collapse
- #[](name) ⇒ Object
- #format_one(record) ⇒ Object
-
#initialize(name, mode = :nullable) ⇒ RecordSchema
constructor
A new instance of RecordSchema.
- #load_schema(schema, allow_overwrite = true) ⇒ Object
- #register_field(name, type) ⇒ Object
- #to_a ⇒ Object
- #to_h ⇒ Object
- #type ⇒ Object
Methods inherited from FieldSchema
Constructor Details
#initialize(name, mode = :nullable) ⇒ RecordSchema
Returns a new instance of RecordSchema.
512 513 514 515 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 512 def initialize(name, mode = :nullable) super(name, mode) @fields = {} end |
Instance Method Details
#[](name) ⇒ Object
521 522 523 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 521 def [](name) @fields[name] end |
#format_one(record) ⇒ Object
578 579 580 581 582 583 584 585 586 587 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 578 def format_one(record) out = {} @fields.each do |key, schema| value = record[key] formatted = schema.format(value) next if formatted.nil? # field does not exists, or null value out[key] = formatted end out end |
#load_schema(schema, allow_overwrite = true) ⇒ Object
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 540 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
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 562 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_a ⇒ Object
525 526 527 528 529 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 525 def to_a @fields.map do |_, field_schema| field_schema.to_h end end |
#to_h ⇒ Object
531 532 533 534 535 536 537 538 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 531 def to_h { 'name' => name, 'type' => type.to_s.upcase, 'mode' => mode.to_s.upcase, 'fields' => self.to_a, } end |
#type ⇒ Object
517 518 519 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 517 def type :record end |