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) ⇒ Object
- #register_field(name, type) ⇒ Object
- #type ⇒ Object
Methods inherited from FieldSchema
Constructor Details
#initialize(name, mode = :nullable) ⇒ RecordSchema
Returns a new instance of RecordSchema.
413 414 415 416 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 413 def initialize(name, mode = :nullable) super(name, mode) @fields = {} end |
Instance Method Details
#[](name) ⇒ Object
422 423 424 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 422 def [](name) @fields[name] end |
#format_one(record) ⇒ Object
462 463 464 465 466 467 468 469 470 471 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 462 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) ⇒ Object
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 426 def load_schema(schema) 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 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']) end end end |
#register_field(name, type) ⇒ Object
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 446 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 |
#type ⇒ Object
418 419 420 |
# File 'lib/fluent/plugin/out_bigquery.rb', line 418 def type :record end |