rbhive 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,77 @@
1
+ require File.join(File.dirname(__FILE__), *%w[.. thrift thrift_hive])
2
+
3
+ module RBHive
4
+ def connect(server, port=10_000)
5
+ connection = RBHive::Connection.new(server, port)
6
+ begin
7
+ connection.open
8
+ yield(connection)
9
+ ensure
10
+ connection.close
11
+ end
12
+ end
13
+ module_function :connect
14
+
15
+ class Connection
16
+ attr_reader :client
17
+ def initialize(server, port=10_000)
18
+ @socket = Thrift::Socket.new(server, port)
19
+ @transport = Thrift::BufferedTransport.new(@socket)
20
+ @protocol = Thrift::BinaryProtocol.new(@transport)
21
+ @client = ThriftHive::Client.new(@protocol)
22
+ end
23
+
24
+ def open
25
+ @transport.open
26
+ end
27
+
28
+ def close
29
+ @transport.close
30
+ end
31
+
32
+ def client
33
+ @client
34
+ end
35
+
36
+ def execute(query)
37
+ client.execute(query)
38
+ end
39
+
40
+ def fetch(query)
41
+ execute(query)
42
+ ResultSet.new(client.fetchAll)
43
+ end
44
+
45
+ def first(query)
46
+ execute(query)
47
+ ResultSet.new([client.fetchOne])
48
+ end
49
+
50
+ def method_missing(meth, *args)
51
+ client.send(meth, *args)
52
+ end
53
+ end
54
+
55
+ class ResultSet < Array
56
+ def initialize(rows)
57
+ super(rows.map {|r| r.split("\t") })
58
+ end
59
+
60
+ def to_csv(out_file=nil)
61
+ output(",", out_file)
62
+ end
63
+
64
+ def to_tsv(out_file=nil)
65
+ output("\t", out_file)
66
+ end
67
+
68
+ private
69
+
70
+ def output(sep, out_file)
71
+ tsv = self.map { |r| r.join("\t") }.join("\n")
72
+ return tsv if out_file.nil?
73
+ File.open(out_file, 'w') { |f| f << tsv }
74
+ end
75
+ end
76
+ end
77
+
data/lib/rbhive.rb ADDED
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'rbhive', 'connection')