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, boolean: BooleanFieldSchema, timestamp: TimestampFieldSchema, date: DateFieldSchema, datetime: DateTimeFieldSchema, time: TimeFieldSchema, 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.
180 181 182 183 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 180 def initialize(name, mode = :nullable) super(name, mode) @fields = {} end |
Instance Method Details
#[](name) ⇒ Object
189 190 191 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 189 def [](name) @fields[name] end |
#empty? ⇒ Boolean
193 194 195 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 193 def empty? @fields.empty? end |
#format_one(record) ⇒ Object
250 251 252 253 254 255 256 257 258 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 250 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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 212 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
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 234 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
197 198 199 200 201 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 197 def to_a @fields.map do |_, field_schema| field_schema.to_h end end |
#to_h ⇒ Object
203 204 205 206 207 208 209 210 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 203 def to_h { :name => name, :type => type.to_s.upcase, :mode => mode.to_s.upcase, :fields => self.to_a, } end |
#type ⇒ Object
185 186 187 |
# File 'lib/fluent/plugin/bigquery/schema.rb', line 185 def type :record end |