Class: OkHbase::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ok_hbase/connection.rb

Constant Summary collapse

DEFAULT_OPTS =
{
    host: 'localhost',
    port: 9090,
    timeout: 5,
    auto_connect: false,
    table_prefix: nil,
    table_prefix_separator: '_',
    transport: :buffered,
    max_tries: 3
}.freeze
THRIFT_TRANSPORTS =
{
    buffered: Thrift::BufferedTransport,
    framed: Thrift::FramedTransport,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Connection

Returns a new instance of Connection.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ok_hbase/connection.rb', line 34

def initialize(opts={})
  opts = DEFAULT_OPTS.merge opts

  raise ArgumentError.new ":transport must be one of: #{THRIFT_TRANSPORTS.keys}" unless THRIFT_TRANSPORTS.keys.include?(opts[:transport])
  raise TypeError.new ":table_prefix must be a string" if opts[:table_prefix] && !opts[:table_prefix].is_a?(String)
  raise TypeError.new ":table_prefix_separator must be a string" unless opts[:table_prefix_separator].is_a?(String)


  @host = opts[:host]
  @port = opts[:port]
  @timeout = opts[:timeout]
  @max_tries = opts[:max_tries]
  @auto_connect = opts[:auto_connect]
  @table_prefix = opts[:table_prefix]
  @table_prefix_separator = opts[:table_prefix_separator]
  @transport_class = THRIFT_TRANSPORTS[opts[:transport]]

  _refresh_thrift_client
  open if @auto_connect

end

Instance Attribute Details

#auto_connectObject

Returns the value of attribute auto_connect.



31
32
33
# File 'lib/ok_hbase/connection.rb', line 31

def auto_connect
  @auto_connect
end

#clientObject (readonly)

Returns the value of attribute client.



32
33
34
# File 'lib/ok_hbase/connection.rb', line 32

def client
  @client
end

#hostObject

Returns the value of attribute host.



31
32
33
# File 'lib/ok_hbase/connection.rb', line 31

def host
  @host
end

#max_triesObject

Returns the value of attribute max_tries.



31
32
33
# File 'lib/ok_hbase/connection.rb', line 31

def max_tries
  @max_tries
end

#portObject

Returns the value of attribute port.



31
32
33
# File 'lib/ok_hbase/connection.rb', line 31

def port
  @port
end

#table_prefixObject

Returns the value of attribute table_prefix.



31
32
33
# File 'lib/ok_hbase/connection.rb', line 31

def table_prefix
  @table_prefix
end

#table_prefix_separatorObject

Returns the value of attribute table_prefix_separator.



31
32
33
# File 'lib/ok_hbase/connection.rb', line 31

def table_prefix_separator
  @table_prefix_separator
end

#timeoutObject

Returns the value of attribute timeout.



31
32
33
# File 'lib/ok_hbase/connection.rb', line 31

def timeout
  @timeout
end

Instance Method Details

#closeObject



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

def close
  return unless open?
  @transport.close
end

#compact_table(name, major = false) ⇒ Object



138
139
140
141
142
# File 'lib/ok_hbase/connection.rb', line 138

def compact_table(name, major=false)
  name = table_name(name)

  major ? client.majorCompact(name) : client.compact(name)
end

#create_table(name, families) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ok_hbase/connection.rb', line 87

def create_table(name, families)
  name = table_name(name)

  raise ArgumentError.new "Can't create table #{name}. (no column families specified)" unless families
  raise TypeError.new "families' arg must be a hash" unless families.respond_to?(:[])

  column_descriptors = []

  families.each_pair do |family_name, options|
    options ||= {}

    args = {}
    options.each_pair do |option_name, value|
      args[option_name.to_s.camelcase(:lower)] = value
    end

    family_name = "#{family_name}:" unless family_name.to_s.end_with? ':'
    args[:name] = family_name

    column_descriptors << Apache::Hadoop::Hbase::Thrift::ColumnDescriptor.new(args)
  end

  client.createTable(name, column_descriptors)
  table(name)
end

#delete_table(name, disable = false) ⇒ Object



113
114
115
116
117
118
# File 'lib/ok_hbase/connection.rb', line 113

def delete_table(name, disable=false)
  name = table_name(name)

  disable_table(name) if disable && table_enabled?(name)
  client.deleteTable(name)
end

#disable_table(name) ⇒ Object



126
127
128
129
130
# File 'lib/ok_hbase/connection.rb', line 126

def disable_table(name)
  name = table_name(name)

  client.disableTable(name)
end

#enable_table(name) ⇒ Object



120
121
122
123
124
# File 'lib/ok_hbase/connection.rb', line 120

def enable_table(name)
  name = table_name(name)

  client.enableTable(name)
end

#openObject



56
57
58
59
60
61
# File 'lib/ok_hbase/connection.rb', line 56

def open
  return if open?
  @transport.open

  OkHbase.logger.info "OkHbase connected"
end

#open?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ok_hbase/connection.rb', line 63

def open?
  @transport && @transport.open?
end

#table(name, use_prefix = true) ⇒ Object



72
73
74
75
# File 'lib/ok_hbase/connection.rb', line 72

def table(name, use_prefix=true)
  name = table_name(name) if use_prefix
  OkHbase::Table.new(name, self)
end

#table_enabled?(name) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/ok_hbase/connection.rb', line 132

def table_enabled?(name)
  name = table_name(name)

  client.isTableEnabled(name)
end

#table_name(name) ⇒ Object



144
145
146
# File 'lib/ok_hbase/connection.rb', line 144

def table_name(name)
  table_prefix && !name.start_with?(table_prefix) ? [table_prefix, name].join(table_prefix_separator) : name
end

#tablesObject



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

def tables
  names = client.getTableNames
  if table_prefix
    names = names.map do |n|
      n["#{table_prefix}#{table_prefix_separator}".size..-1] if n.start_with?(table_prefix)
    end
  end
  names
end