Class: Fluent::BigQuery::RecordSchema
- Inherits:
-
FieldSchema
- Object
- FieldSchema
- Fluent::BigQuery::RecordSchema
- Defined in:
- lib/fluent/plugin/bigquery/schema.rb
Constant Summary collapse
- FIELD_TYPES =
{ string: StringFieldSchema, integer: IntegerFieldSchema, float: FloatFieldSchema, numeric: NumericFieldSchema, boolean: BooleanFieldSchema, timestamp: TimestampFieldSchema, date: DateFieldSchema, datetime: DateTimeFieldSchema, time: TimeFieldSchema, json: JsonFieldSchema, geography: GeographyFieldSchema, record: RecordSchema }.freeze
Instance Attribute Summary
Attributes inherited from FieldSchema
Instance Method Summary collapse
- #[](name) ⇒ Object
- #empty? ⇒ Boolean
- #format_one(record, is_load: false) ⇒ Object
-
#initialize(name, mode = :nullable) ⇒ RecordSchema
constructor
A new instance of RecordSchema.
- #load_schema(schema) ⇒ 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.
213 214 215 216 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 213 def initialize(name, mode = :nullable) super(name, mode) @fields = {} end |
Instance Method Details
#[](name) ⇒ Object
222 223 224 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 222 def [](name) @fields[name] end |
#empty? ⇒ Boolean
226 227 228 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 226 def empty? @fields.empty? end |
#format_one(record, is_load: false) ⇒ Object
281 282 283 284 285 286 287 288 289 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 281 def format_one(record, is_load: false) out = {} record.each do |key, value| next if value.nil? schema = @fields[key] out[key] = schema ? schema.format(value, is_load: is_load) : value end out end |
#load_schema(schema) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 245 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
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 265 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
230 231 232 233 234 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 230 def to_a @fields.map do |_, field_schema| field_schema.to_h end end |
#to_h ⇒ Object
236 237 238 239 240 241 242 243 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 236 def to_h { :name => name, :type => type.to_s.upcase, :mode => mode.to_s.upcase, :fields => self.to_a, } end |
#type ⇒ Object
218 219 220 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 218 def type :record end |