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, options = {}) ⇒ Connection

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



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

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

Instance Method Details

#closeObject

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



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

def close
  return unless @connected

  @transport.close
  @connected = false
end

#execute(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:



78
79
80
81
82
83
84
# File 'lib/impala/connection.rb', line 78

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

  handle = send_query(query, query_options)
  check_result(handle)
  Cursor.new(handle, @service)
end

#inspectObject



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

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

#openObject

Open the connection if it’s currently closed.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/impala/connection.rb', line 21

def open
  return if @connected

  socket = Thrift::Socket.new(@host, @port, @options[:timeout])

  if @options[:kerberos]
    @transport = SASLTransport.new(socket, :GSSAPI, @options[:kerberos])
  elsif @options[:sasl]
    @transport = SASLTransport.new(socket, :PLAIN, @options[:sasl])
  else
    @transport = Thrift::BufferedTransport.new(socket)
  end

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


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

def open?
  @connected
end

#query(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.



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

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

#refreshObject

Refresh the metadata store.

Raises:



55
56
57
58
# File 'lib/impala/connection.rb', line 55

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