Class: Impala::Connection
- Inherits:
-
Object
- Object
- Impala::Connection
- 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
-
#close ⇒ Object
Close this connection.
-
#execute(query, query_options = {}) ⇒ Cursor
Perform a query and return a cursor for iterating over the results.
-
#initialize(host, port, options = {}) ⇒ Connection
constructor
Don’t instantiate Connections directly; instead, use connect.
- #inspect ⇒ Object
-
#open ⇒ Object
Open the connection if it’s currently closed.
-
#open? ⇒ Boolean
Returns true if the connection is currently open.
-
#query(query, query_options = {}) ⇒ Array<Hash>
Perform a query and return all the results.
-
#refresh ⇒ Object
Refresh the metadata store.
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, ={}) @host = host @port = port @options = @connected = false open end |
Instance Method Details
#close ⇒ Object
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.
78 79 80 81 82 83 84 |
# File 'lib/impala/connection.rb', line 78 def execute(query, = {}) raise ConnectionError.new("Connection closed") unless open? handle = send_query(query, ) check_result(handle) Cursor.new(handle, @service) end |
#inspect ⇒ Object
16 17 18 |
# File 'lib/impala/connection.rb', line 16 def inspect "#<#{self.class} #{@host}:#{@port}#{open? ? '' : ' (DISCONNECTED)'}>" end |
#open ⇒ Object
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.
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.
68 69 70 |
# File 'lib/impala/connection.rb', line 68 def query(query, = {}) execute(query, ).fetch_all end |
#refresh ⇒ Object
Refresh the metadata store.
55 56 57 58 |
# File 'lib/impala/connection.rb', line 55 def refresh raise ConnectionError.new("Connection closed") unless open? @service.ResetCatalog end |