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(raw_query, query_options = {}) ⇒ Cursor
Perform a query and return a cursor for iterating over the results.
-
#initialize(host, port) ⇒ 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(raw_query, query_options = {}) ⇒ Array<Hash>
Perform a query and return all the results.
-
#refresh ⇒ Object
Refresh the metadata store.
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
#close ⇒ Object
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.
70 71 72 73 74 75 76 77 78 |
# File 'lib/impala/connection.rb', line 70 def execute(raw_query, = {}) raise ConnectionError.new("Connection closed") unless open? query = sanitize_query(raw_query) handle = send_query(query, ) check_result(handle) Cursor.new(handle, @service) end |
#inspect ⇒ Object
15 16 17 |
# File 'lib/impala/connection.rb', line 15 def inspect "#<#{self.class} #{@host}:#{@port}#{open? ? '' : ' (DISCONNECTED)'}>" end |
#open ⇒ Object
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.
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.
60 61 62 |
# File 'lib/impala/connection.rb', line 60 def query(raw_query, = {}) execute(raw_query, ).fetch_all end |
#refresh ⇒ Object
Refresh the metadata store.
47 48 49 50 |
# File 'lib/impala/connection.rb', line 47 def refresh raise ConnectionError.new("Connection closed") unless open? @service.ResetCatalog end |