Class: RBHive::TableSchema
- Inherits:
-
Object
- Object
- RBHive::TableSchema
- Defined in:
- lib/rbhive/table_schema.rb
Defined Under Namespace
Classes: Column
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#name ⇒ Object
Returns the value of attribute name.
-
#partitions ⇒ Object
readonly
Returns the value of attribute partitions.
Instance Method Summary collapse
- #add_columns_statement ⇒ Object
- #column(name, type, comment = nil) ⇒ Object
- #create_table_statement ⇒ Object
- #delimited_statement ⇒ Object
-
#initialize(name, comment = nil, options = {}, &blk) ⇒ TableSchema
constructor
A new instance of TableSchema.
- #partition(name, type, comment = nil) ⇒ Object
- #replace_columns_statement ⇒ Object
- #row_format_statement ⇒ Object
- #serde(name, properties = {}) ⇒ Object
- #serde_properties_statement ⇒ Object
- #serde_statement ⇒ Object
- #stored_as ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, comment = nil, options = {}, &blk) ⇒ TableSchema
Returns a new instance of TableSchema.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/rbhive/table_schema.rb', line 5 def initialize(name, comment=nil, ={}, &blk) @name, @comment = name, comment @location = [:location] || nil @field_sep = [:field_sep] || "\t" @line_sep = [:line_sep] || "\n" @collection_sep = [:collection_sep] || "|" @stored_as = [:stored_as] || :textfile @columns = [] @partitions = [] @serde_name = nil @serde_properties = {} instance_eval(&blk) if blk end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
4 5 6 |
# File 'lib/rbhive/table_schema.rb', line 4 def columns @columns end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/rbhive/table_schema.rb', line 3 def name @name end |
#partitions ⇒ Object (readonly)
Returns the value of attribute partitions.
4 5 6 |
# File 'lib/rbhive/table_schema.rb', line 4 def partitions @partitions end |
Instance Method Details
#add_columns_statement ⇒ Object
72 73 74 |
# File 'lib/rbhive/table_schema.rb', line 72 def add_columns_statement alter_columns_statement("ADD") end |
#column(name, type, comment = nil) ⇒ Object
19 20 21 |
# File 'lib/rbhive/table_schema.rb', line 19 def column(name, type, comment=nil) @columns << Column.new(name, type, comment) end |
#create_table_statement ⇒ Object
32 33 34 35 36 37 |
# File 'lib/rbhive/table_schema.rb', line 32 def create_table_statement() %[CREATE #{external}TABLE #{table_statement} ROW FORMAT #{row_format_statement} STORED AS #{stored_as} #{location}] end |
#delimited_statement ⇒ Object
51 52 53 54 55 56 |
# File 'lib/rbhive/table_schema.rb', line 51 def delimited_statement %(DELIMITED FIELDS TERMINATED BY '#{@field_sep}' COLLECTION ITEMS TERMINATED BY '#{@collection_sep}' LINES TERMINATED BY '#{@line_sep}') end |
#partition(name, type, comment = nil) ⇒ Object
23 24 25 |
# File 'lib/rbhive/table_schema.rb', line 23 def partition(name, type, comment=nil) @partitions << Column.new(name, type, comment) end |
#replace_columns_statement ⇒ Object
68 69 70 |
# File 'lib/rbhive/table_schema.rb', line 68 def replace_columns_statement alter_columns_statement("REPLACE") end |
#row_format_statement ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/rbhive/table_schema.rb', line 43 def row_format_statement if @serde_name serde_statement else delimited_statement end end |
#serde(name, properties = {}) ⇒ Object
27 28 29 30 |
# File 'lib/rbhive/table_schema.rb', line 27 def serde(name, properties={}) @serde_name = name @serde_properties = properties end |
#serde_properties_statement ⇒ Object
62 63 64 65 66 |
# File 'lib/rbhive/table_schema.rb', line 62 def serde_properties_statement return '' unless @serde_properties.any? kvs = @serde_properties.map { |k,v| %("#{k}" = "#{v}") }.join(",\n") %(WITH SERDEPROPERTIES (#{kvs})) end |
#serde_statement ⇒ Object
58 59 60 |
# File 'lib/rbhive/table_schema.rb', line 58 def serde_statement %(SERDE '#{@serde_name}'\n#{serde_properties_statement}) end |
#stored_as ⇒ Object
39 40 41 |
# File 'lib/rbhive/table_schema.rb', line 39 def stored_as @stored_as.to_s.upcase end |
#to_s ⇒ Object
76 77 78 |
# File 'lib/rbhive/table_schema.rb', line 76 def to_s table_statement end |