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
- #empty? ⇒ Boolean
- #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.
770 771 772 773 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 770 def initialize(name, mode = :nullable) super(name, mode) @fields = {} end |
Instance Method Details
#[](name) ⇒ Object
779 780 781 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 779 def [](name) @fields[name] end |
#empty? ⇒ Boolean
783 784 785 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 783 def empty? @fields.empty? end |
#format_one(record) ⇒ Object
840 841 842 843 844 845 846 847 848 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 840 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
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 802 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
824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 824 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
787 788 789 790 791 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 787 def to_a @fields.map do |_, field_schema| field_schema.to_h end end |
#to_h ⇒ Object
793 794 795 796 797 798 799 800 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 793 def to_h { :name => name, :type => type.to_s.upcase, :mode => mode.to_s.upcase, :fields => self.to_a, } end |
#type ⇒ Object
775 776 777 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 775 def type :record end |