Class: RBHive::Connection
- Inherits:
-
Object
- Object
- RBHive::Connection
show all
- Defined in:
- lib/rbhive/connection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(server, port = 10_000, logger = StdOutLogger.new) ⇒ Connection
Returns a new instance of Connection.
33
34
35
36
37
38
39
40
41
|
# File 'lib/rbhive/connection.rb', line 33
def initialize(server, port=10_000, logger=StdOutLogger.new)
@socket = Thrift::Socket.new(server, port)
@transport = Thrift::BufferedTransport.new(@socket)
@protocol = Thrift::BinaryProtocol.new(@transport)
@client = ThriftHive::Client.new(@protocol)
@logger = logger
@logger.info("Connecting to #{server} on port #{port}")
@mutex = Mutex.new
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args) ⇒ Object
117
118
119
|
# File 'lib/rbhive/connection.rb', line 117
def method_missing(meth, *args)
client.send(meth, *args)
end
|
Instance Attribute Details
#client ⇒ Object
Returns the value of attribute client.
31
32
33
|
# File 'lib/rbhive/connection.rb', line 31
def client
@client
end
|
Instance Method Details
#add_columns(schema) ⇒ Object
113
114
115
|
# File 'lib/rbhive/connection.rb', line 113
def add_columns(schema)
execute(schema.add_columns_statement)
end
|
#close ⇒ Object
47
48
49
|
# File 'lib/rbhive/connection.rb', line 47
def close
@transport.close
end
|
#create_table(schema) ⇒ Object
100
101
102
|
# File 'lib/rbhive/connection.rb', line 100
def create_table(schema)
execute(schema.create_table_statement)
end
|
#drop_table(name) ⇒ Object
104
105
106
107
|
# File 'lib/rbhive/connection.rb', line 104
def drop_table(name)
name = name.name if name.is_a?(TableSchema)
execute("DROP TABLE `#{name}`")
end
|
#execute(query) ⇒ Object
55
56
57
|
# File 'lib/rbhive/connection.rb', line 55
def execute(query)
execute_safe(query)
end
|
#fetch(query) ⇒ Object
72
73
74
75
76
77
78
79
|
# File 'lib/rbhive/connection.rb', line 72
def fetch(query)
safe do
execute_unsafe(query)
rows = client.fetchAll
schema = SchemaDefinition.new(client.getSchema, rows.first)
ResultSet.new(rows, schema)
end
end
|
#fetch_in_batch(query, batch_size = 1_000) ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/rbhive/connection.rb', line 81
def fetch_in_batch(query, batch_size=1_000)
safe do
execute_unsafe(query)
until (next_batch = client.fetchN(batch_size)).empty?
schema ||= SchemaDefinition.new(client.getSchema, next_batch.first)
yield ResultSet.new(next_batch, schema)
end
end
end
|
#first(query) ⇒ Object
91
92
93
94
95
96
97
98
|
# File 'lib/rbhive/connection.rb', line 91
def first(query)
safe do
execute_unsafe(query)
row = client.fetchOne
schema = SchemaDefinition.new(client.getSchema, row)
ResultSet.new([row], schema).first
end
end
|
#open ⇒ Object
43
44
45
|
# File 'lib/rbhive/connection.rb', line 43
def open
@transport.open
end
|
#priority=(priority) ⇒ Object
59
60
61
|
# File 'lib/rbhive/connection.rb', line 59
def priority=(priority)
set("mapred.job.priority", priority)
end
|
#queue=(queue) ⇒ Object
63
64
65
|
# File 'lib/rbhive/connection.rb', line 63
def queue=(queue)
set("mapred.job.queue.name", queue)
end
|
#replace_columns(schema) ⇒ Object
109
110
111
|
# File 'lib/rbhive/connection.rb', line 109
def replace_columns(schema)
execute(schema.replace_columns_statement)
end
|
#set(name, value) ⇒ Object
67
68
69
70
|
# File 'lib/rbhive/connection.rb', line 67
def set(name,value)
@logger.info("Setting #{name}=#{value}")
client.execute("SET #{name}=#{value}")
end
|