Class: Fluent::BigQuery::RecordSchema

Inherits:
FieldSchema show all
Defined in:
lib/fluent/plugin/bigquery/schema.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.



129
130
131
132
# File 'lib/fluent/plugin/bigquery/schema.rb', line 129

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

Instance Method Details

#[](name) ⇒ Object



138
139
140
# File 'lib/fluent/plugin/bigquery/schema.rb', line 138

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

#empty?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/fluent/plugin/bigquery/schema.rb', line 142

def empty?
  @fields.empty?
end

#format_one(record) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/fluent/plugin/bigquery/schema.rb', line 199

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/fluent/plugin/bigquery/schema.rb', line 161

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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/fluent/plugin/bigquery/schema.rb', line 183

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



146
147
148
149
150
# File 'lib/fluent/plugin/bigquery/schema.rb', line 146

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

#to_hObject



152
153
154
155
156
157
158
159
# File 'lib/fluent/plugin/bigquery/schema.rb', line 152

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

#typeObject



134
135
136
# File 'lib/fluent/plugin/bigquery/schema.rb', line 134

def type
  :record
end