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
 5 def initialize(name, comment=nil, options={}, &blk)
 6   @name, @comment = name, comment
 7   @location = options[:location] || nil
 8   @field_sep = options[:field_sep] || "\t"
 9   @line_sep = options[:line_sep] || "\n"
10   @collection_sep = options[:collection_sep] || "|"
11   @stored_as = options[:stored_as] || :textfile
12   @columns = []
13   @partitions = []
14   @serde_name = nil
15   @serde_properties = {}
16   instance_eval(&blk) if blk
17 end

Public Instance Methods

add_columns_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
72 def add_columns_statement
73   alter_columns_statement("ADD")
74 end
column(name, type, comment=nil) click to toggle source
   # File lib/rbhive/table_schema.rb
19 def column(name, type, comment=nil)
20   @columns << Column.new(name, type, comment)
21 end
create_table_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
32   def create_table_statement()
33     %[CREATE #{external}TABLE #{table_statement}
34 ROW FORMAT #{row_format_statement}
35 STORED AS #{stored_as}
36 #{location}]
37   end
delimited_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
51   def delimited_statement
52     %(DELIMITED
53 FIELDS TERMINATED BY '#{@field_sep}'
54 COLLECTION ITEMS TERMINATED BY '#{@collection_sep}'
55 LINES TERMINATED BY '#{@line_sep}')
56   end
partition(name, type, comment=nil) click to toggle source
   # File lib/rbhive/table_schema.rb
23 def partition(name, type, comment=nil)
24   @partitions << Column.new(name, type, comment)
25 end
replace_columns_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
68 def replace_columns_statement
69   alter_columns_statement("REPLACE")
70 end
row_format_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
43 def row_format_statement
44   if @serde_name
45     serde_statement
46   else
47     delimited_statement
48   end
49 end
serde(name, properties={}) click to toggle source
   # File lib/rbhive/table_schema.rb
27 def serde(name, properties={})
28   @serde_name = name
29   @serde_properties = properties
30 end
serde_properties_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
62 def serde_properties_statement
63   return '' unless @serde_properties.any?
64   kvs = @serde_properties.map { |k,v| %("#{k}" = "#{v}") }.join(",\n")
65   %(WITH SERDEPROPERTIES (#{kvs}))
66 end
serde_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
58 def serde_statement
59   %(SERDE '#{@serde_name}'\n#{serde_properties_statement})
60 end
stored_as() click to toggle source
   # File lib/rbhive/table_schema.rb
39 def stored_as
40   @stored_as.to_s.upcase
41 end
to_s() click to toggle source
   # File lib/rbhive/table_schema.rb
76 def to_s
77   table_statement
78 end

Private Instance Methods

alter_columns_statement(add_or_replace) click to toggle source
   # File lib/rbhive/table_schema.rb
95 def alter_columns_statement(add_or_replace)
96   %[ALTER TABLE `#{name}` #{add_or_replace} COLUMNS #{column_statement}]
97 end
column_statement() click to toggle source
    # File lib/rbhive/table_schema.rb
 99 def column_statement
100   cols = @columns.join(",\n")
101   "(\n#{cols}\n)"
102 end
external() click to toggle source
   # File lib/rbhive/table_schema.rb
82 def external
83   @location.nil? ? '' : 'EXTERNAL '
84 end
location() click to toggle source
   # File lib/rbhive/table_schema.rb
91 def location
92   @location.nil? ? '' : "LOCATION '#{@location}'"
93 end
partition_statement() click to toggle source
    # File lib/rbhive/table_schema.rb
104 def partition_statement
105   return "" if @partitions.nil? || @partitions.empty?
106   cols = @partitions.join(",\n")
107   "PARTITIONED BY (\n#{cols}\n)"
108 end
table_statement() click to toggle source
   # File lib/rbhive/table_schema.rb
86 def table_statement
87   comment_string = (@comment.nil? ? '' : " COMMENT '#{@comment}'")
88   %[`#{@name}` #{column_statement}#{comment_string}\n#{partition_statement}]
89 end