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
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



128
129
130
# File 'lib/rbhive/connection.rb', line 128

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



124
125
126
# File 'lib/rbhive/connection.rb', line 124

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

#closeObject



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

def close
  @transport.close
end

#create_table(schema) ⇒ Object



111
112
113
# File 'lib/rbhive/connection.rb', line 111

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

#drop_table(name) ⇒ Object



115
116
117
118
# File 'lib/rbhive/connection.rb', line 115

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

#explain(query) ⇒ Object



59
60
61
62
63
64
# File 'lib/rbhive/connection.rb', line 59

def explain(query)
  safe do
    execute_unsafe("EXPLAIN "+ query)
    ExplainResult.new(client.fetchAll)
  end
end

#fetch(query) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/rbhive/connection.rb', line 79

def fetch(query)
  safe do
    execute_unsafe(query)
    rows = client.fetchAll
    the_schema = SchemaDefinition.new(client.getSchema, rows.first)
    ResultSet.new(rows, the_schema)
  end
end

#fetch_in_batch(query, batch_size = 1_000) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/rbhive/connection.rb', line 88

def fetch_in_batch(query, batch_size=1_000)
  safe do
    execute_unsafe(query)
    until (next_batch = client.fetchN(batch_size)).empty?
      the_schema ||= SchemaDefinition.new(client.getSchema, next_batch.first)
      yield ResultSet.new(next_batch, the_schema)
    end
  end
end

#first(query) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/rbhive/connection.rb', line 98

def first(query)
  safe do
    execute_unsafe(query)
    row = client.fetchOne
    the_schema = SchemaDefinition.new(client.getSchema, row)
    ResultSet.new([row], the_schema).first
  end
end

#openObject



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

def open
  @transport.open
end

#priority=(priority) ⇒ Object



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

def priority=(priority)
  set("mapred.job.priority", priority)
end

#queue=(queue) ⇒ Object



70
71
72
# File 'lib/rbhive/connection.rb', line 70

def queue=(queue)
  set("mapred.job.queue.name", queue)
end

#replace_columns(schema) ⇒ Object



120
121
122
# File 'lib/rbhive/connection.rb', line 120

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

#schema(example_row = []) ⇒ Object



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

def schema(example_row=[])
  safe { SchemaDefinition.new(client.getSchema, example_row) }
end

#set(name, value) ⇒ Object



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

def set(name,value)
  @logger.info("Setting #{name}=#{value}")
  client.execute("SET #{name}=#{value}")
end