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.



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

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.



29
30
31
# File 'lib/ok_hbase/connection.rb', line 29

def auto_connect
  @auto_connect
end

#clientObject (readonly)

Returns the value of attribute client.



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

def client
  @client
end

#hostObject

Returns the value of attribute host.



29
30
31
# File 'lib/ok_hbase/connection.rb', line 29

def host
  @host
end

#max_triesObject

Returns the value of attribute max_tries.



29
30
31
# File 'lib/ok_hbase/connection.rb', line 29

def max_tries
  @max_tries
end

#portObject

Returns the value of attribute port.



29
30
31
# File 'lib/ok_hbase/connection.rb', line 29

def port
  @port
end

#table_prefixObject

Returns the value of attribute table_prefix.



29
30
31
# File 'lib/ok_hbase/connection.rb', line 29

def table_prefix
  @table_prefix
end

#table_prefix_separatorObject

Returns the value of attribute table_prefix_separator.



29
30
31
# File 'lib/ok_hbase/connection.rb', line 29

def table_prefix_separator
  @table_prefix_separator
end

#timeoutObject

Returns the value of attribute timeout.



29
30
31
# File 'lib/ok_hbase/connection.rb', line 29

def timeout
  @timeout
end

Instance Method Details

#closeObject



65
66
67
68
# File 'lib/ok_hbase/connection.rb', line 65

def close
  return unless open?
  @transport.close
end

#compact_table(name, major = false) ⇒ Object



136
137
138
139
140
# File 'lib/ok_hbase/connection.rb', line 136

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

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

#create_table(name, families) ⇒ Object



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

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



111
112
113
114
115
116
# File 'lib/ok_hbase/connection.rb', line 111

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



124
125
126
127
128
# File 'lib/ok_hbase/connection.rb', line 124

def disable_table(name)
  name = table_name(name)

  client.disableTable(name)
end

#enable_table(name) ⇒ Object



118
119
120
121
122
# File 'lib/ok_hbase/connection.rb', line 118

def enable_table(name)
  name = table_name(name)

  client.enableTable(name)
end

#openObject



54
55
56
57
58
59
# File 'lib/ok_hbase/connection.rb', line 54

def open
  return if open?
  @transport.open

  OkHbase.logger.info "OkHbase connected"
end

#open?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ok_hbase/connection.rb', line 61

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

#table(name, use_prefix = true) ⇒ Object



70
71
72
73
# File 'lib/ok_hbase/connection.rb', line 70

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)


130
131
132
133
134
# File 'lib/ok_hbase/connection.rb', line 130

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

  client.isTableEnabled(name)
end

#table_name(name) ⇒ Object



142
143
144
# File 'lib/ok_hbase/connection.rb', line 142

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

#tablesObject



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

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