Class: MassiveRecord::Adapters::Thrift::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/adapters/thrift/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Returns a new instance of Connection.



8
9
10
11
12
# File 'lib/massive_record/adapters/thrift/connection.rb', line 8

def initialize(opts = {})
  @timeout = 4000
  @host    = opts[:host]
  @port    = opts[:port] || 9090
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Wrapp HBase API to be able to catch errors and try reconnect



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/massive_record/adapters/thrift/connection.rb', line 54

def method_missing(method, *args)
  begin
    open if not @client
    client.send(method, *args) if @client
  rescue IOError
    @client = nil
    open
    client.send(method, *args) if @client
  rescue ::Thrift::TransportException
    @transport = nil
    @client = nil
    open
    client.send(method, *args) if @client
  end
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/massive_record/adapters/thrift/connection.rb', line 6

def host
  @host
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/massive_record/adapters/thrift/connection.rb', line 6

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/massive_record/adapters/thrift/connection.rb', line 6

def timeout
  @timeout
end

Instance Method Details

#clientObject



34
35
36
# File 'lib/massive_record/adapters/thrift/connection.rb', line 34

def client
  @client
end

#closeObject



30
31
32
# File 'lib/massive_record/adapters/thrift/connection.rb', line 30

def close
  @transport.close.nil?
end

#load_table(table_name) ⇒ Object



49
50
51
# File 'lib/massive_record/adapters/thrift/connection.rb', line 49

def load_table(table_name)
  MassiveRecord::Wrapper::Table.new(self, table_name)
end

#openObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/massive_record/adapters/thrift/connection.rb', line 18

def open
  protocol = ::Thrift::BinaryProtocol.new(transport)
  @client = Apache::Hadoop::Hbase::Thrift::Hbase::Client.new(protocol)

  begin
    transport.open()
    true
  rescue
    raise MassiveRecord::Wrapper::Errors::ConnectionException.new, "Unable to connect to HBase on #{@host}, port #{@port}"
  end
end

#open?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/massive_record/adapters/thrift/connection.rb', line 38

def open?
  @transport.try("open?")
end

#tablesObject



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

def tables
  collection = MassiveRecord::Wrapper::TablesCollection.new
  collection.connection = self
  getTableNames().each{|table_name| collection.push(table_name)}
  collection
end

#transportObject



14
15
16
# File 'lib/massive_record/adapters/thrift/connection.rb', line 14

def transport
  @transport ||= ::Thrift::BufferedTransport.new(::Thrift::Socket.new(@host, @port, @timeout))
end