rbhive 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -24,6 +24,7 @@ module RBHive
24
24
 
25
25
  class Connection
26
26
  attr_reader :client
27
+
27
28
  def initialize(server, port=10_000, logger=StdOutLogger.new)
28
29
  @socket = Thrift::Socket.new(server, port)
29
30
  @transport = Thrift::BufferedTransport.new(@socket)
@@ -59,6 +60,19 @@ module RBHive
59
60
  ResultSet.new([client.fetchOne])
60
61
  end
61
62
 
63
+ def create_table(schema)
64
+ execute(schema.create_table_statement)
65
+ end
66
+
67
+ def drop_table(name)
68
+ name = name.name if name.is_a?(TableSchema)
69
+ execute("DROP TABLE `#{name}`")
70
+ end
71
+
72
+ def replace_columns(schema)
73
+ execute(schema.replace_columns_statement)
74
+ end
75
+
62
76
  def method_missing(meth, *args)
63
77
  client.send(meth, *args)
64
78
  end
@@ -0,0 +1,63 @@
1
+ class TableSchema
2
+ attr_accessor :name
3
+ attr_reader :columns, :partitions
4
+ def initialize(name, comment=nil, field_sep='\t', line_sep='\n', &blk)
5
+ @name, @comment, @field_sep, @line_sep = name, comment, field_sep, line_sep
6
+ @columns = []
7
+ @partitions = []
8
+ instance_eval(&blk) if blk
9
+ end
10
+
11
+ def column(name, type, comment=nil)
12
+ @columns << Column.new(name, type, comment)
13
+ end
14
+
15
+ def partition(name, type, comment=nil)
16
+ @partitions << Column.new(name, type, comment)
17
+ end
18
+
19
+ def create_table_statement()
20
+ %[CREATE TABLE #{table_statement}
21
+ ROW FORMAT DELIMITED
22
+ FIELDS TERMINATED BY '#{@field_sep}'
23
+ LINES TERMINATED BY '#{@line_sep}'
24
+ STORED AS TEXTFILE]
25
+ end
26
+
27
+ def replace_columns_statement()
28
+ %[ALTER TABLE `#{name}` REPLACE COLUMNS #{column_statement}]
29
+ end
30
+
31
+ def to_s
32
+ table_statement
33
+ end
34
+
35
+ private
36
+
37
+ def table_statement
38
+ comment_string = (@comment.nil? ? '' : " COMMENT '#{@comment}'")
39
+ %[`#{@name}` #{column_statement}#{comment_string}\n#{partition_statement}]
40
+ end
41
+
42
+ def column_statement
43
+ cols = @columns.join(",\n")
44
+ "(\n#{cols}\n)"
45
+ end
46
+
47
+ def partition_statement
48
+ cols = @partitions.join(",\n")
49
+ "PARTITIONED BY (\n#{cols}\n)"
50
+ end
51
+
52
+ class Column
53
+ attr_reader :name, :type, :comment
54
+ def initialize(name, type, comment=nil)
55
+ @name, @type, @comment = name, type, comment
56
+ end
57
+
58
+ def to_s
59
+ comment_string = @comment.nil? ? '' : " COMMENT '#{@comment}'"
60
+ "`#{@name}` #{@type.to_s.upcase}#{comment_string}"
61
+ end
62
+ end
63
+ end
data/lib/rbhive.rb CHANGED
@@ -1 +1,2 @@
1
- require File.join(File.dirname(__FILE__), 'rbhive', 'connection')
1
+ require File.join(File.dirname(__FILE__), 'rbhive', 'connection')
2
+ require File.join(File.dirname(__FILE__), 'rbhive', 'schema')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rbhive
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 4
10
- version: 0.1.4
9
+ - 5
10
+ version: 0.1.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Forward Internet Group
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-29 00:00:00 +01:00
18
+ date: 2010-10-01 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -45,6 +45,7 @@ extra_rdoc_files: []
45
45
  files:
46
46
  - lib/rbhive.rb
47
47
  - lib/rbhive/connection.rb
48
+ - lib/rbhive/schema.rb
48
49
  - lib/thrift/facebook_service.rb
49
50
  - lib/thrift/fb303_constants.rb
50
51
  - lib/thrift/fb303_types.rb