Class: HBase::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Admin, Scoped::Aggregation::Admin, Util
Defined in:
lib/hbase-jruby/table.rb,
lib/hbase-jruby/table/admin.rb,
lib/hbase-jruby/table/inspection.rb

Overview

@return [org.apache.hadoop.conf.Configuration]

Constant Summary

Constants included from Util

Util::JAVA_BYTE_ARRAY_CLASS, Util::JAVA_BYTE_ARRAY_EMPTY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

append_0, from_bytes, java_bytes?, parse_column_name, to_bytes

Methods included from Scoped::Aggregation::Admin

#enable_aggregation, #enable_aggregation!

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/hbase-jruby/table.rb', line 11

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/hbase-jruby/table.rb', line 10

def name
  @name
end

Instance Method Details

#add_coprocessor(class_name, props = {}) ⇒ Object

Adds the table coprocessor to the table (asynchronous)



173
174
175
# File 'lib/hbase-jruby/table/admin.rb', line 173

def add_coprocessor class_name, props = {}
  _add_coprocessor class_name, props, false
end

#add_coprocessor!(class_name, props = {}) {|progress, total| ... } ⇒ Object

Adds the table coprocessor to the table

Parameters:

  • class_name (String)

    Full class name of the coprocessor

  • props (Hash) (defaults to: {})

    Coprocessor properties

Options Hash (props):

  • path (String)

    The path of the JAR file

  • priority (Fixnum)

    Coprocessor priority

  • params (Hash<#to_s, #to_s>)

    Arbitrary key-value parameter pairs passed into the coprocessor

Yields:

  • (progress, total)

Yield Parameters:

  • progress (Fixnum)

    Number of regions updated

  • total (Fixnum)

    Total number of regions



168
169
170
# File 'lib/hbase-jruby/table/admin.rb', line 168

def add_coprocessor! class_name, props = {}, &block
  _add_coprocessor class_name, props, true, &block
end

#add_family(name, opts) ⇒ Object

Adds the column family (asynchronous)

See Also:



122
123
124
# File 'lib/hbase-jruby/table/admin.rb', line 122

def add_family name, opts
  _add_family name, opts, false
end

#add_family!(name, opts) {|progress, total| ... } ⇒ nil

Adds the column family (synchronous)

Parameters:

  • name (#to_s)

    The name of the column family

  • opts (Hash)

    Column family properties

Yields:

  • (progress, total)

Yield Parameters:

  • progress (Fixnum)

    Number of regions updated

  • total (Fixnum)

    Total number of regions

Returns:

  • (nil)


116
117
118
# File 'lib/hbase-jruby/table/admin.rb', line 116

def add_family! name, opts, &block
  _add_family name, opts, true, &block
end

#alter(props) ⇒ Object

Alters the table (asynchronous)

See Also:



105
106
107
# File 'lib/hbase-jruby/table/admin.rb', line 105

def alter props
  _alter props, false
end

#alter!(props) {|progress, total| ... } ⇒ nil

Alters the table (synchronous)

Examples:

table.alter!(
  :max_filesize       => 512 * 1024 ** 2,
  :memstore_flushsize =>  64 * 1024 ** 2,
  :readonly           => false,
  :deferred_log_flush => true
)

Parameters:

  • props (Hash)

    Table properties

Yields:

  • (progress, total)

Yield Parameters:

  • progress (Fixnum)

    Number of regions updated

  • total (Fixnum)

    Total number of regions

Returns:

  • (nil)


99
100
101
# File 'lib/hbase-jruby/table/admin.rb', line 99

def alter! props, &block
  _alter props, true, &block
end

#alter_family(name, opts) ⇒ Object

Alters the column family (asynchronous)

See Also:



139
140
141
# File 'lib/hbase-jruby/table/admin.rb', line 139

def alter_family name, opts
  _alter_family name, opts, false
end

#alter_family!(name, opts) {|progress, total| ... } ⇒ nil

Alters the column family

Parameters:

  • name (#to_s)

    The name of the column family

  • opts (Hash)

    Column family properties

Yields:

  • (progress, total)

Yield Parameters:

  • progress (Fixnum)

    Number of regions updated

  • total (Fixnum)

    Total number of regions

Returns:

  • (nil)


133
134
135
# File 'lib/hbase-jruby/table/admin.rb', line 133

def alter_family! name, opts, &block
  _alter_family name, opts, true, &block
end

#closenil

Deprecated.

Returns:

  • (nil)


29
30
31
# File 'lib/hbase-jruby/table.rb', line 29

def close
  nil
end

#create!(column_family_name, props = {}) ⇒ nil #create!(column_family_hash, props = {}) ⇒ nil #create!(table_descriptor) ⇒ nil

Creates the table

Overloads:

  • #create!(column_family_name, props = {}) ⇒ nil

    Create the table with one column family of the given name

    Parameters:

    • The (#to_s)

      name of the column family

    • props (Hash) (defaults to: {})

      Table properties

    Returns:

    • (nil)
  • #create!(column_family_hash, props = {}) ⇒ nil

    Create the table with the specified column families

    Examples:

    table.create!(
      # Column family with default options
      :cf1 => {},
      # Another column family with custom properties
      :cf2 => {
        :blockcache         => true,
        :blocksize          => 128 * 1024,
        :bloomfilter        => :row,
        :compression        => :snappy,
        :in_memory          => true,
        :keep_deleted_cells => true,
        :min_versions       => 2,
        :replication_scope  => 0,
        :ttl                => 100,
        :versions           => 5
      }
    )

    Parameters:

    • Column (Hash)

      family properties

    • props (Hash) (defaults to: {})

      Table properties

    Returns:

    • (nil)
  • #create!(table_descriptor) ⇒ nil

    Create the table with the given HTableDescriptor

    Parameters:

    • Table (org.apache.hadoop.hbase.HTableDescriptor)

      descriptor

    Returns:

    • (nil)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hbase-jruby/table/admin.rb', line 54

def create! desc, props = {}
  splits =
    if props[:splits]
      raise ArgumentError, ":splits property must be an Array" if !props[:splits].is_a?(Array)
      props[:splits].map { |e| Util.to_bytes(e).to_a }.to_java(Java::byte[])
    end

  todo = nil
  with_admin do |admin|
    raise RuntimeError, 'Table already exists' if admin.tableExists(@name)

    case desc
    when HTableDescriptor
      patch_table_descriptor! desc, props
      admin.createTable *[desc, splits].compact
    when Symbol, String
      todo = lambda { create!({desc => {}}, props) }
    when Hash
      htd = HTableDescriptor.new(@name.to_java_bytes)
      patch_table_descriptor! htd, props
      desc.each do |name, opts|
        htd.addFamily hcd(name, opts)
      end

      admin.createTable *[htd, splits].compact
    else
      raise ArgumentError, 'Invalid table description'
    end
  end
  todo.call if todo # Avoids mutex relocking
end

#delete(rowkey) ⇒ nil #delete(rowkey, column_family) ⇒ nil #delete(rowkey, column) ⇒ nil #delete(rowkey, column, *timestamps) ⇒ nil #delete(*delete_specs) ⇒ nil

Deletes data

Overloads:

  • #delete(rowkey) ⇒ nil

    Deletes a row with the given rowkey

    Examples:

    table.delete('a000')

    Parameters:

    • rowkey (Object)

    Returns:

    • (nil)
  • #delete(rowkey, column_family) ⇒ nil

    Deletes columns with the given column family from the row

    Examples:

    table.delete('a000', 'cf1')

    Parameters:

    • rowkey (Object)
    • column_family (String)

    Returns:

    • (nil)
  • #delete(rowkey, column) ⇒ nil

    Deletes a column

    Examples:

    table.delete('a000', 'cf1:col1')

    Parameters:

    • rowkey (Object)
    • column (String, Array)

      Column expression in String “FAMILY:QUALIFIER”, or in Array [FAMILY, QUALIFIER]

    Returns:

    • (nil)
  • #delete(rowkey, column, *timestamps) ⇒ nil

    Deletes specified versions of a column

    Examples:

    table.delete('a000', 'cf1:col1', 1352978648642)

    Parameters:

    • rowkey (Object)
    • column (String, Array)

      Column expression in String “FAMILY:QUALIFIER”, or in Array [FAMILY, QUALIFIER]

    • timestamps (*Fixnum)

      Timestamps.

    Returns:

    • (nil)
  • #delete(*delete_specs) ⇒ nil

    Batch deletion

    Examples:

    table.delete(
      ['a000', 'cf1:col1', 1352978648642],
      ['a001', 'cf1:col1'],
      ['a002', 'cf1'],
      ['a003'])

    Parameters:

    • delete_specs (Array<Array>)

    Returns:

    • (nil)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/hbase-jruby/table.rb', line 108

def delete *args
  specs = args.first.is_a?(Array) ? args : [args]

  htable.delete specs.map { |spec|
    rowkey, cfcq, *ts = spec
    cf, cq = Util.parse_column_name(cfcq) if cfcq

    Delete.new(Util.to_bytes rowkey).tap { |del|
      if !ts.empty?
        ts.each do |t|
          del.deleteColumn cf, cq, time_to_long(t)
        end
      elsif cq
        # Delete all versions
        del.deleteColumns cf, cq
      elsif cf
        del.deleteFamily cf
      end
    }
  }
end

#delete_family(name) ⇒ Object

Removes the column family (asynchronous)

See Also:



155
156
157
# File 'lib/hbase-jruby/table/admin.rb', line 155

def delete_family name
  _delete_family name, false
end

#delete_family!(name) {|progress, total| ... } ⇒ nil

Removes the column family

Parameters:

  • name (#to_s)

    The name of the column family

Yields:

  • (progress, total)

Yield Parameters:

  • progress (Fixnum)

    Number of regions updated

  • total (Fixnum)

    Total number of regions

Returns:

  • (nil)


149
150
151
# File 'lib/hbase-jruby/table/admin.rb', line 149

def delete_family! name, &block
  _delete_family name, true, &block
end

#delete_row(*rowkeys) ⇒ nil

Delete rows.

Parameters:

  • rowkeys (*Object)

    List of rowkeys of rows to delete

Returns:

  • (nil)


133
134
135
# File 'lib/hbase-jruby/table.rb', line 133

def delete_row *rowkeys
  htable.delete rowkeys.map { |rk| Delete.new(Util.to_bytes rk) }
end

#descriptororg.apache.hadoop.hbase.client.UnmodifyableHTableDescriptor

Returns a read-only org.apache.hadoop.hbase.HTableDescriptor object

Returns:

  • (org.apache.hadoop.hbase.client.UnmodifyableHTableDescriptor)


5
6
7
# File 'lib/hbase-jruby/table/inspection.rb', line 5

def descriptor
  htable.get_table_descriptor
end

#disable!nil

Disables the table

Returns:

  • (nil)


217
218
219
220
221
# File 'lib/hbase-jruby/table/admin.rb', line 217

def disable!
  with_admin do |admin|
    admin.disableTable @name if admin.isTableEnabled(@name)
  end
end

#disabled?true, false

Checks if the table is disabled

Returns:

  • (true, false)

    Whether table is disabled



17
18
19
# File 'lib/hbase-jruby/table/admin.rb', line 17

def disabled?
  !enabled?
end

#drop!nil

Drops the table

Returns:

  • (nil)


233
234
235
236
237
238
239
240
241
# File 'lib/hbase-jruby/table/admin.rb', line 233

def drop!
  with_admin do |admin|
    raise RuntimeError, 'Table does not exist' unless admin.tableExists @name

    admin.disableTable @name if admin.isTableEnabled(@name)
    admin.deleteTable  @name
    close
  end
end

#each {|row| ... } ⇒ HBase::Scoped

Scan through the table

Yields:

  • (row)

    Yields each row in the scope

Yield Parameters:

Returns:



172
173
174
175
176
177
178
179
180
# File 'lib/hbase-jruby/table.rb', line 172

def each
  check_closed

  if block_given?
    Scoped.send(:new, self).each { |r| yield r }
  else
    Scoped.send(:new, self)
  end
end

#enable!nil

Enables the table

Returns:

  • (nil)


209
210
211
212
213
# File 'lib/hbase-jruby/table/admin.rb', line 209

def enable!
  with_admin do |admin|
    admin.enableTable @name unless admin.isTableEnabled(@name)
  end
end

#enabled?true, false

Checks if the table is enabled

Returns:

  • (true, false)

    Whether table is enabled



11
12
13
# File 'lib/hbase-jruby/table/admin.rb', line 11

def enabled?
  with_admin { |admin| admin.isTableEnabled(@name) }
end

#exists?true, false

Checks if the table of the name exists

Returns:

  • (true, false)

    Whether table exists



5
6
7
# File 'lib/hbase-jruby/table/admin.rb', line 5

def exists?
  with_admin { |admin| admin.tableExists @name }
end

#familiesHash

Returns properties of column families indexed by family name

Returns:

  • (Hash)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/hbase-jruby/table/inspection.rb', line 31

def families
  {}.tap { |ret|
    descriptor.families.each do |family|
      name = family.name_as_string
      ret[name] =
        {}.tap { |props|
          COLUMN_PROPERTIES.each do |prop, gs|
            get = gs[:get]
            if get && family.respond_to?(get)
              props[prop] = parse_property family.send get
            end
          end
        }
    end
  }
end

#has_coprocessor?(class_name) ⇒ true, false

Return if the table has the coprocessor of the given class name

Parameters:

  • class_name (String)

    Full class name of the coprocessor

Returns:

  • (true, false)


196
197
198
# File 'lib/hbase-jruby/table/admin.rb', line 196

def has_coprocessor? class_name
  descriptor.hasCoprocessor(class_name)
end

#htableorg.apache.hadoop.hbase.client.HTable

(INTERNAL) Returns the underlying org.apache.hadoop.hbase.client.HTable object (local to current thread)

Returns:

  • (org.apache.hadoop.hbase.client.HTable)


20
21
22
23
24
25
# File 'lib/hbase-jruby/table.rb', line 20

def htable
  check_closed

  local_htables = Thread.current[:htable] ||= {}
  local_htables[@name] ||= @pool.get_table(@name)
end

#increment(rowkey, column, by) ⇒ Fixnum #increment(rowkey, column_by_hash) ⇒ Object

Atomically increase numeric values

Overloads:

  • #increment(rowkey, column, by) ⇒ Fixnum

    Atomically increase column value by the specified amount

    Examples:

    table.increment('a000', 'cf1:col1', 1)

    Parameters:

    • rowkey (Object)

      Rowkey

    • column (String, Array)

      Column expression in String “FAMILY:QUALIFIER”, or in Array [FAMILY, QUALIFIER]

    • by (Fixnum)

      Increment amount

    Returns:

    • (Fixnum)

      Column value after increment

  • #increment(rowkey, column_by_hash) ⇒ Object

    Atomically increase values of multiple columns

    Examples:

    table.increment('a000', 'cf1:col1' => 1, 'cf1:col2' => 2)

    Parameters:

    • rowkey (Object)

      Rowkey

    • column_by_hash (Hash)

      Column expression to increment amount pairs



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/hbase-jruby/table.rb', line 152

def increment rowkey, *args
  if args.first.is_a?(Hash)
    cols = args.first
    htable.increment Increment.new(Util.to_bytes rowkey).tap { |inc|
      cols.each do |col, by|
        cf, cq = Util.parse_column_name(col)
        inc.addColumn cf, cq, by
      end
    }
  else
    col, by = args
    cf, cq = Util.parse_column_name(col)
    htable.incrementColumnValue Util.to_bytes(rowkey), cf, cq, by || 1
  end
end

#inspectString

Returns a printable version of the table description

Returns:

  • (String)

    Table description



69
70
71
72
73
74
75
76
# File 'lib/hbase-jruby/table/inspection.rb', line 69

def inspect
  if exists?
    descriptor.toStringCustomizedValues
  else
    # FIXME
    "{NAME => '#{@name}'}"
  end
end

#propertiesHash

Returns table properties

Returns:

  • (Hash)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/hbase-jruby/table/inspection.rb', line 11

def properties
  desc = descriptor
  {}.tap { |props|
    TABLE_PROPERTIES.each do |prop, gs|
      get = gs[:get]
      if get && desc.respond_to?(get)
        props[prop] = parse_property desc.send get
      end
    end
  }
end

#put(rowkey, data) ⇒ Fixnum #put(data) ⇒ Fixnum

Performs PUT operations

Overloads:

  • #put(rowkey, data) ⇒ Fixnum

    Put operation on a rowkey

    Parameters:

    • rowkey (Object)

      Rowkey

    • data (Hash)

      Data to put

    Returns:

    • (Fixnum)

      Number of puts succeeded

  • #put(data) ⇒ Fixnum

    Put operation on multiple rowkeys

    Parameters:

    • data (Hash<Hash>)

      Data to put indexed by rowkeys

    Returns:

    • (Fixnum)

      Number of puts succeeded



61
62
63
64
65
66
67
# File 'lib/hbase-jruby/table.rb', line 61

def put *args
  return put(args.first => args.last) if args.length == 2

  puts = args.first.map { |rowkey, props| putify rowkey, props }
  htable.put puts
  puts.length
end

#raw_familiesHash

Returns raw String-to-String map of column family properties indexed by name

Returns:

  • (Hash)


50
51
52
53
54
55
56
57
# File 'lib/hbase-jruby/table/inspection.rb', line 50

def raw_families
  {}.tap { |ret|
    descriptor.families.each do |family|
      name = family.name_as_string
      ret[name] = parse_raw_map family.values
    end
  }
end

#raw_propertiesHash

Returns raw String-to-String map of table properties

Returns:

  • (Hash)


25
26
27
# File 'lib/hbase-jruby/table/inspection.rb', line 25

def raw_properties
  parse_raw_map descriptor.values
end

#regionsHash

Returns region information

Returns:

  • (Hash)


61
62
63
64
65
# File 'lib/hbase-jruby/table/inspection.rb', line 61

def regions
  with_admin do |admin|
    _regions admin
  end
end

#remove_coprocessor(class_name) ⇒ Object

Removes the coprocessor from the table (asynchronous)



189
190
191
# File 'lib/hbase-jruby/table/admin.rb', line 189

def remove_coprocessor class_name
  _remove_coprocessor class_name, false
end

#remove_coprocessor!(class_name) {|progress, total| ... } ⇒ nil

Removes the coprocessor from the table.

Parameters:

  • class_name (String)

    Full class name of the coprocessor

Yields:

  • (progress, total)

Yield Parameters:

  • progress (Fixnum)

    Number of regions updated

  • total (Fixnum)

    Total number of regions

Returns:

  • (nil)


183
184
185
# File 'lib/hbase-jruby/table/admin.rb', line 183

def remove_coprocessor! class_name, &block
  _remove_coprocessor class_name, true, &block
end

#split!(*split_keys) ⇒ nil

Splits the table region on the given split point (asynchronous)

Parameters:

  • split_keys (*Object)

Returns:

  • (nil)


203
204
205
# File 'lib/hbase-jruby/table/admin.rb', line 203

def split! *split_keys
  _split split_keys, false
end

#truncate!nil

Truncates the table by dropping it and recreating it.

Returns:

  • (nil)


225
226
227
228
229
# File 'lib/hbase-jruby/table/admin.rb', line 225

def truncate!
  htd = htable.get_table_descriptor
  drop!
  create! htd
end

#with_java_get(&block) ⇒ Object



47
48
49
# File 'lib/hbase-jruby/table.rb', line 47

def with_java_get &block
  self.each.with_java_get(&block)
end

#with_java_scan(&block) ⇒ Object



43
44
45
# File 'lib/hbase-jruby/table.rb', line 43

def with_java_scan &block
  self.each.with_java_scan(&block)
end