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

LOG_CONTEXT_ID =
"impala-ruby"

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, query_options = {}) ⇒ Cursor

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

Parameters:

  • query (String)

    the query you want to run

  • query_options (Hash) (defaults to: {})

    the options to set user and configuration except for :user, see TImpalaQueryOptions in ImpalaService.thrift

Options Hash (query_options):

  • :user (String)

    the user runs the query

Returns:

  • (Cursor)

    a cursor for the result rows

Raises:



70
71
72
73
74
75
76
77
78
# File 'lib/impala/connection.rb', line 70

def execute(raw_query, query_options = {})
  raise ConnectionError.new("Connection closed") unless open?

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

  check_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, query_options = {}) ⇒ 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

  • query_options (Hash) (defaults to: {})

    the options to set user and configuration except for :user, see TImpalaQueryOptions in ImpalaService.thrift

Options Hash (query_options):

  • :user (String)

    the user runs the query

Returns:

  • (Array<Hash>)

    an array of hashes, one for each row.



60
61
62
# File 'lib/impala/connection.rb', line 60

def query(raw_query, query_options = {})
  execute(raw_query, query_options).fetch_all
end

#refreshObject

Refresh the metadata store.

Raises:



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

def refresh
  raise ConnectionError.new("Connection closed") unless open?
  @service.ResetCatalog
end