Class: RBHive::SchemaDefinition
- Inherits:
-
Object
- Object
- RBHive::SchemaDefinition
- Defined in:
- lib/rbhive/schema_definition.rb
Constant Summary collapse
- TYPES =
{ :boolean => :to_s, :string => :to_s, :bigint => :to_i, :float => :to_f, :double => :to_f, :int => :to_i, :smallint => :to_i, :tinyint => :to_i, }
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Instance Method Summary collapse
- #coerce_column(column_name, value) ⇒ Object
- #coerce_row(row) ⇒ Object
- #coerce_row_to_array(row) ⇒ Object
- #column_names ⇒ Object
- #column_type_map ⇒ Object
-
#initialize(schema, example_row) ⇒ SchemaDefinition
constructor
A new instance of SchemaDefinition.
Constructor Details
#initialize(schema, example_row) ⇒ SchemaDefinition
Returns a new instance of SchemaDefinition.
16 17 18 19 |
# File 'lib/rbhive/schema_definition.rb', line 16 def initialize(schema, example_row) @schema = schema @example_row = example_row ? example_row.split("\t") : [] end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
3 4 5 |
# File 'lib/rbhive/schema_definition.rb', line 3 def schema @schema end |
Instance Method Details
#coerce_column(column_name, value) ⇒ Object
50 51 52 53 54 |
# File 'lib/rbhive/schema_definition.rb', line 50 def coerce_column(column_name, value) type = column_type_map[column_name] conversion_method = TYPES[type] conversion_method ? value.send(conversion_method) : value end |
#coerce_row(row) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/rbhive/schema_definition.rb', line 43 def coerce_row(row) column_names.zip(row.split("\t")).inject({}) do |hsh, (column_name, value)| hsh[column_name] = coerce_column(column_name, value) hsh end end |
#coerce_row_to_array(row) ⇒ Object
56 57 58 |
# File 'lib/rbhive/schema_definition.rb', line 56 def coerce_row_to_array(row) column_names.map { |n| row[n] } end |
#column_names ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rbhive/schema_definition.rb', line 21 def column_names @column_names ||= begin schema_names = @schema.fieldSchemas.map {|c| c.name.to_sym } # Lets fix the fact that Hive doesn't return schema data for partitions on SELECT * queries # For now we will call them :_p1, :_p2, etc. to avoid collisions. offset = 0 while schema_names.length < @example_row.length schema_names.push(:"_p#{offset+=1}") end schema_names end end |
#column_type_map ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/rbhive/schema_definition.rb', line 34 def column_type_map @column_type_map ||= column_names.inject({}) do |hsh, c| definition = @schema.fieldSchemas.find {|s| s.name.to_sym == c } # If the column isn't in the schema (eg partitions in SELECT * queries) assume they are strings hsh[c] = definition ? definition.type.to_sym : :string hsh end end |