Class: RBHive::Connection

Inherits:
Object
  • Object
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.



28
29
30
31
32
33
34
# File 'lib/rbhive/connection.rb', line 28

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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



76
77
78
# File 'lib/rbhive/connection.rb', line 76

def method_missing(meth, *args)
  client.send(meth, *args)
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



26
27
28
# File 'lib/rbhive/connection.rb', line 26

def client
  @client
end

Instance Method Details

#closeObject



40
41
42
# File 'lib/rbhive/connection.rb', line 40

def close
  @transport.close
end

#create_table(schema) ⇒ Object



63
64
65
# File 'lib/rbhive/connection.rb', line 63

def create_table(schema)
  execute(schema.create_table_statement)
end

#drop_table(name) ⇒ Object



67
68
69
70
# File 'lib/rbhive/connection.rb', line 67

def drop_table(name)
  name = name.name if name.is_a?(TableSchema)
  execute("DROP TABLE `#{name}`")
end

#execute(query) ⇒ Object



48
49
50
51
# File 'lib/rbhive/connection.rb', line 48

def execute(query)
  @logger.info("Executing Hive Query: #{query}")
  client.execute(query)
end

#fetch(query) ⇒ Object



53
54
55
56
# File 'lib/rbhive/connection.rb', line 53

def fetch(query)
  execute(query)
  ResultSet.new(client.fetchAll)
end

#first(query) ⇒ Object



58
59
60
61
# File 'lib/rbhive/connection.rb', line 58

def first(query)
  execute(query)
  ResultSet.new([client.fetchOne])
end

#openObject



36
37
38
# File 'lib/rbhive/connection.rb', line 36

def open
  @transport.open
end

#replace_columns(schema) ⇒ Object



72
73
74
# File 'lib/rbhive/connection.rb', line 72

def replace_columns(schema)
  execute(schema.replace_columns_statement)
end