rbhive 0.2.93 → 0.2.94

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/rbhive/table_schema.rb +46 -17
  2. metadata +5 -5
@@ -10,45 +10,74 @@ module RBHive
10
10
  @collection_sep = options[:collection_sep] || "|"
11
11
  @columns = []
12
12
  @partitions = []
13
+ @serde_name = nil
14
+ @serde_properties = {}
13
15
  instance_eval(&blk) if blk
14
16
  end
15
-
17
+
16
18
  def column(name, type, comment=nil)
17
19
  @columns << Column.new(name, type, comment)
18
20
  end
19
-
21
+
20
22
  def partition(name, type, comment=nil)
21
23
  @partitions << Column.new(name, type, comment)
22
24
  end
23
-
25
+
26
+ def serde(name, properties={})
27
+ @serde_name = name
28
+ @serde_properties = properties
29
+ end
30
+
24
31
  def create_table_statement()
25
32
  %[CREATE #{external}TABLE #{table_statement}
26
- ROW FORMAT DELIMITED
27
- FIELDS TERMINATED BY '#{@field_sep}'
28
- COLLECTION ITEMS TERMINATED BY '#{@collection_sep}'
29
- LINES TERMINATED BY '#{@line_sep}'
33
+ ROW FORMAT #{row_format_statement}
30
34
  STORED AS TEXTFILE
31
35
  #{location}]
32
36
  end
33
-
37
+
38
+ def row_format_statement
39
+ if @serde_name
40
+ serde_statement
41
+ else
42
+ delimited_statement
43
+ end
44
+ end
45
+
46
+ def delimited_statement
47
+ %(DELIMITED
48
+ FIELDS TERMINATED BY '#{@field_sep}'
49
+ COLLECTION ITEMS TERMINATED BY '#{@collection_sep}'
50
+ LINES TERMINATED BY '#{@line_sep}')
51
+ end
52
+
53
+ def serde_statement
54
+ %(SERDE '#{@serde_name}'\n#{serde_properties_statement})
55
+ end
56
+
57
+ def serde_properties_statement
58
+ return '' unless @serde_properties.any?
59
+ kvs = @serde_properties.map { |k,v| %("#{k}" = "#{v}") }.join(",\n")
60
+ %(WITH SERDEPROPERTIES (#{kvs}))
61
+ end
62
+
34
63
  def replace_columns_statement
35
64
  alter_columns_statement("REPLACE")
36
65
  end
37
-
66
+
38
67
  def add_columns_statement
39
68
  alter_columns_statement("ADD")
40
69
  end
41
-
70
+
42
71
  def to_s
43
72
  table_statement
44
73
  end
45
-
74
+
46
75
  private
47
76
 
48
77
  def external
49
78
  @location.nil? ? '' : 'EXTERNAL '
50
79
  end
51
-
80
+
52
81
  def table_statement
53
82
  comment_string = (@comment.nil? ? '' : " COMMENT '#{@comment}'")
54
83
  %[`#{@name}` #{column_statement}#{comment_string}\n#{partition_statement}]
@@ -57,28 +86,28 @@ module RBHive
57
86
  def location
58
87
  @location.nil? ? '' : "LOCATION '#{@location}'"
59
88
  end
60
-
89
+
61
90
  def alter_columns_statement(add_or_replace)
62
91
  %[ALTER TABLE `#{name}` #{add_or_replace} COLUMNS #{column_statement}]
63
92
  end
64
-
93
+
65
94
  def column_statement
66
95
  cols = @columns.join(",\n")
67
96
  "(\n#{cols}\n)"
68
97
  end
69
-
98
+
70
99
  def partition_statement
71
100
  return "" if @partitions.nil? || @partitions.empty?
72
101
  cols = @partitions.join(",\n")
73
102
  "PARTITIONED BY (\n#{cols}\n)"
74
103
  end
75
-
104
+
76
105
  class Column
77
106
  attr_reader :name, :type, :comment
78
107
  def initialize(name, type, comment=nil)
79
108
  @name, @type, @comment = name, type, comment
80
109
  end
81
-
110
+
82
111
  def to_s
83
112
  comment_string = @comment.nil? ? '' : " COMMENT '#{@comment}'"
84
113
  "`#{@name}` #{@type.to_s.upcase}#{comment_string}"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbhive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.93
4
+ version: 0.2.94
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2010-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: thrift
16
- requirement: &70263196714240 !ruby/object:Gem::Requirement
16
+ requirement: &70366900708200 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.4.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70263196714240
24
+ version_requirements: *70366900708200
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: json
27
- requirement: &70263196713840 !ruby/object:Gem::Requirement
27
+ requirement: &70366900707140 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70263196713840
35
+ version_requirements: *70366900707140
36
36
  description: Simple lib for executing Hive queries
37
37
  email: andy@forward.co.uk
38
38
  executables: []