class RBHive::TableSchema
Attributes
columns[R]
name[RW]
partitions[R]
Public Class Methods
new(name, comment=nil, options={}, &blk)
click to toggle source
# File lib/rbhive/table_schema.rb, line 5 def initialize(name, comment=nil, options={}, &blk) @name, @comment = name, comment @location = options[:location] || nil @field_sep = options[:field_sep] || "\t" @line_sep = options[:line_sep] || "\n" @collection_sep = options[:collection_sep] || "|" @stored_as = options[:stored_as] || :textfile @columns = [] @partitions = [] @serde_name = nil @serde_properties = {} instance_eval(&blk) if blk end
Public Instance Methods
add_columns_statement()
click to toggle source
# File lib/rbhive/table_schema.rb, line 72 def add_columns_statement alter_columns_statement("ADD") end
column(name, type, comment=nil)
click to toggle source
# File lib/rbhive/table_schema.rb, line 19 def column(name, type, comment=nil) @columns << Column.new(name, type, comment) end
create_table_statement()
click to toggle source
# 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()
click to toggle source
# 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)
click to toggle source
# File lib/rbhive/table_schema.rb, line 23 def partition(name, type, comment=nil) @partitions << Column.new(name, type, comment) end
replace_columns_statement()
click to toggle source
# File lib/rbhive/table_schema.rb, line 68 def replace_columns_statement alter_columns_statement("REPLACE") end
row_format_statement()
click to toggle source
# 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={})
click to toggle source
# File lib/rbhive/table_schema.rb, line 27 def serde(name, properties={}) @serde_name = name @serde_properties = properties end
serde_properties_statement()
click to toggle source
# 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()
click to toggle source
# File lib/rbhive/table_schema.rb, line 58 def serde_statement %(SERDE '#{@serde_name}'\n#{serde_properties_statement}) end
stored_as()
click to toggle source
# File lib/rbhive/table_schema.rb, line 39 def stored_as @stored_as.to_s.upcase end
to_s()
click to toggle source
# File lib/rbhive/table_schema.rb, line 76 def to_s table_statement end
Private Instance Methods
alter_columns_statement(add_or_replace)
click to toggle source
# File lib/rbhive/table_schema.rb, line 95 def alter_columns_statement(add_or_replace) %[ALTER TABLE `#{name}` #{add_or_replace} COLUMNS #{column_statement}] end
column_statement()
click to toggle source
# File lib/rbhive/table_schema.rb, line 99 def column_statement cols = @columns.join(",\n") "(\n#{cols}\n)" end
external()
click to toggle source
# File lib/rbhive/table_schema.rb, line 82 def external @location.nil? ? '' : 'EXTERNAL ' end
location()
click to toggle source
# File lib/rbhive/table_schema.rb, line 91 def location @location.nil? ? '' : "LOCATION '#{@location}'" end
partition_statement()
click to toggle source
# File lib/rbhive/table_schema.rb, line 104 def partition_statement return "" if @partitions.nil? || @partitions.empty? cols = @partitions.join(",\n") "PARTITIONED BY (\n#{cols}\n)" end
table_statement()
click to toggle source
# File lib/rbhive/table_schema.rb, line 86 def table_statement comment_string = (@comment.nil? ? '' : " COMMENT '#{@comment}'") %[`#{@name}` #{column_statement}#{comment_string}\n#{partition_statement}] end