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.



33
34
35
36
37
38
39
40
# 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}")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



106
107
108
# File 'lib/rbhive/connection.rb', line 106

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

Instance Attribute Details

#clientObject (readonly)

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



102
103
104
# File 'lib/rbhive/connection.rb', line 102

def add_columns(schema)
  execute(schema.add_columns_statement)
end

#closeObject



46
47
48
# File 'lib/rbhive/connection.rb', line 46

def close
  @transport.close
end

#create_table(schema) ⇒ Object



89
90
91
# File 'lib/rbhive/connection.rb', line 89

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

#drop_table(name) ⇒ Object



93
94
95
96
# File 'lib/rbhive/connection.rb', line 93

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

#execute(query) ⇒ Object



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

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

#fetch(query) ⇒ Object



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

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

#fetch_in_batch(query, batch_size = 100) ⇒ Object



77
78
79
80
81
82
# File 'lib/rbhive/connection.rb', line 77

def fetch_in_batch(query, batch_size=100)
  execute(query)
  until (next_batch = client.fetchN(batch_size)).empty?
    yield ResultSet.new(next_batch)
  end
end

#first(query) ⇒ Object



84
85
86
87
# File 'lib/rbhive/connection.rb', line 84

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

#openObject



42
43
44
# File 'lib/rbhive/connection.rb', line 42

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



98
99
100
# File 'lib/rbhive/connection.rb', line 98

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