Class: Impala::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/impala/connection.rb

Overview

This object represents a connection to an Impala server. It can be used to perform queries on the database.

Constant Summary collapse

SLEEP_INTERVAL =
0.1

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Connection

Don’t instantiate Connections directly; instead, use Impala.connect.



8
9
10
11
12
13
# File 'lib/impala/connection.rb', line 8

def initialize(host, port)
  @host = host
  @port = port
  @connected = false
  open
end

Instance Method Details

#closeObject

Close this connection. It can still be reopened with #open.



34
35
36
37
38
39
# File 'lib/impala/connection.rb', line 34

def close
  return unless @connected

  @transport.close
  @connected = false
end

#execute(raw_query) ⇒ Cursor

Perform a query and return a cursor for iterating over the results.

Parameters:

  • query (String)

    the query you want to run

Returns:

  • (Cursor)

    a cursor for the result rows

Raises:



58
59
60
61
62
63
64
65
66
# File 'lib/impala/connection.rb', line 58

def execute(raw_query)
  raise ConnectionError.new("Connection closed") unless open?

  query = sanitize_query(raw_query)
  handle = send_query(query)

  wait_for_result(handle)
  Cursor.new(handle, @service)
end

#inspectObject



15
16
17
# File 'lib/impala/connection.rb', line 15

def inspect
  "#<#{self.class} #{@host}:#{@port}#{open? ? '' : ' (DISCONNECTED)'}>"
end

#openObject

Open the connection if it’s currently closed.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/impala/connection.rb', line 20

def open
  return if @connected

  socket = Thrift::Socket.new(@host, @port)

  @transport = Thrift::BufferedTransport.new(socket)
  @transport.open

  proto = Thrift::BinaryProtocol.new(@transport)
  @service = Protocol::ImpalaService::Client.new(proto)
  @connected = true
end

#open?Boolean

Returns true if the connection is currently open.

Returns:

  • (Boolean)


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

def open?
  @connected
end

#query(raw_query) ⇒ Array<Hash>

Perform a query and return all the results. This will load the entire result set into memory, so if you’re dealing with lots of rows, #execute may work better.

Parameters:

  • query (String)

    the query you want to run

Returns:

  • (Array<Hash>)

    an array of hashes, one for each row.



51
52
53
# File 'lib/impala/connection.rb', line 51

def query(raw_query)
  execute(raw_query).fetch_all
end