Class: Hexspace::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/hexspace/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: "localhost", port: nil, username: nil, password: nil, database: nil, mode: :sasl, timeout: nil) ⇒ Client

Returns a new instance of Client.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hexspace/client.rb', line 5

def initialize(host: "localhost", port: nil, username: nil, password: nil, database: nil, mode: :sasl, timeout: nil)
  # could use current user in the future (like beeline)
  username ||= "anonymous"
  password ||= "anonymous"

  # TODO support kerberos
  # TODO support ssl for sockets
  @transport =
    case mode
    when :sasl
      socket = Thrift::Socket.new(host, port || 10000, timeout)
      # TODO support authzid
      SaslTransport.new(socket, username: username, password: password)
    when :nosasl
      socket = Thrift::Socket.new(host, port || 10000, timeout)
      Thrift::BufferedTransport.new(socket)
    when :http, :https
      raise ArgumentError, "timeout not supported with #{mode}" if timeout

      uri_class = mode == :http ? URI::HTTP : URI::HTTPS
      uri = uri_class.build(host: host, port: port || 10001, path: "/cliservice")

      t = Thrift::HTTPClientTransport.new(uri)
      t.add_headers({"Authorization" => "Basic #{Base64.strict_encode64("#{username}:#{password}")}"})
      t
    else
      raise ArgumentError, "Invalid mode: #{mode}"
    end
  @transport.open

  protocol = Thrift::BinaryProtocol.new(@transport)
  @client = TCLIService::Client.new(protocol)

  req = TOpenSessionReq.new
  configuration = {
    # remove table prefix with Hive
    "set:hiveconf:hive.resultset.use.unique.column.names" => "false"
  }
  configuration["use:database"] = database if database
  req.configuration = configuration
  @session = @client.OpenSession(req)
  check_status @session

  ObjectSpace.define_finalizer(self, self.class.finalize(@transport, @client, @session))
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/hexspace/client.rb', line 3

def client
  @client
end

#sessionObject (readonly)

Returns the value of attribute session.



3
4
5
# File 'lib/hexspace/client.rb', line 3

def session
  @session
end

#transportObject (readonly)

Returns the value of attribute transport.



3
4
5
# File 'lib/hexspace/client.rb', line 3

def transport
  @transport
end

Class Method Details

.finalize(transport, client, session) ⇒ Object

private



60
61
62
63
64
65
66
67
# File 'lib/hexspace/client.rb', line 60

def self.finalize(transport, client, session)
  proc do
    req = TCloseSessionReq.new
    req.sessionHandle = session.sessionHandle
    client.CloseSession(req)
    transport.close
  end
end

Instance Method Details

#execute(statement, timeout: nil, result_object: false) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/hexspace/client.rb', line 51

def execute(statement, timeout: nil, result_object: false)
  result = execute_statement(statement, timeout: timeout)
  if result.operationHandle.hasResultSet
    result = process_result(result)
    result_object ? result : result.to_a
  end
end