Class: HBase::Table

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scoped::Aggregation::Admin

#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

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



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/hbase-jruby/table.rb', line 173

def add_coprocessor! class_name, props = {}
  with_admin do |admin|
    while_disabled(admin) do

      htd = admin.get_table_descriptor(@name.to_java_bytes)
      if props.empty?
        htd.addCoprocessor class_name
      else
        path, priority, params = props.values_at :path, :priority, :params
        params = Hash[ params.map { |k, v| [k.to_s, v.to_s] } ]
        htd.addCoprocessor class_name, path, priority || Coprocessor::PRIORITY_USER, params
      end
      admin.modifyTable @name.to_java_bytes, htd
      wait_async_admin(admin)
    end
  end
end

#add_family!(name, opts) ⇒ nil

Adds the column family

Parameters:

  • name (#to_s)

    The name of the column family

  • opts (Hash)

    Column family properties

Returns:

  • (nil)


133
134
135
136
137
138
139
140
# File 'lib/hbase-jruby/table.rb', line 133

def add_family! name, opts
  with_admin do |admin|
    while_disabled(admin) do
      admin.addColumn @name, hcd(name.to_s, opts)
      wait_async_admin(admin)
    end
  end
end

#alter!(props) ⇒ nil

Alters the table

Examples:

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

Parameters:

  • props (Hash)

    Table properties

Returns:

  • (nil)


118
119
120
121
122
123
124
125
126
127
# File 'lib/hbase-jruby/table.rb', line 118

def alter! props
  with_admin do |admin|
    htd = admin.get_table_descriptor(@name.to_java_bytes)
    patch_table_descriptor! htd, props
    while_disabled(admin) do
      admin.modifyTable @name.to_java_bytes, htd
      wait_async_admin(admin)
    end
  end
end

#alter_family!(name, opts) ⇒ nil

Alters the column family

Parameters:

  • name (#to_s)

    The name of the column family

  • opts (Hash)

    Column family properties

Returns:

  • (nil)


146
147
148
149
150
151
152
153
# File 'lib/hbase-jruby/table.rb', line 146

def alter_family! name, opts
  with_admin do |admin|
    while_disabled(admin) do
      admin.modifyColumn @name, hcd(name.to_s, opts)
      wait_async_admin(admin)
    end
  end
end

#closenil

Closes the table and returns HTable object back to the HTablePool.

Returns:

  • (nil)


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

def close
  Thread.current[:htable] ||= {}
  ht = Thread.current[:htable].delete(@name)
  ht.close if ht
  nil
end

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

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)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/hbase-jruby/table.rb', line 82

def create! desc, props = {}
  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
    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
    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, timestamp) ⇒ nil #delete(*delete_specs) ⇒ nil

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, timestamp) ⇒ nil

    Deletes a version 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]

    • timestamp (Fixnum)

      Timestamp.

    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)


315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'lib/hbase-jruby/table.rb', line 315

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, t
        end
      elsif cq
        # Delete all versions
        del.deleteColumns cf, cq
      elsif cf
        del.deleteFamily cf
      end
    }
  }
end

#delete_family!(name) ⇒ nil

Removes the column family

Parameters:

  • name (#to_s)

    The name of the column family

Returns:

  • (nil)


158
159
160
161
162
163
164
165
# File 'lib/hbase-jruby/table.rb', line 158

def delete_family! name
  with_admin do |admin|
    while_disabled(admin) do
      admin.deleteColumn @name, name.to_s
      wait_async_admin(admin)
    end
  end
end

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

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

Returns:

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


19
20
21
# File 'lib/hbase-jruby/table.rb', line 19

def descriptor
  htable.get_table_descriptor
end

#disable!nil

Disables the table

Returns:

  • (nil)


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

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



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

def disabled?
  !enabled?
end

#drop!nil

Drops the table

Returns:

  • (nil)


241
242
243
244
245
246
247
248
249
# File 'lib/hbase-jruby/table.rb', line 241

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 {|HBase::Result| ... } ⇒ HBase::Scoped

Scan through the table

Yields:

Returns:



370
371
372
373
374
375
376
# File 'lib/hbase-jruby/table.rb', line 370

def each
  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)


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

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



40
41
42
# File 'lib/hbase-jruby/table.rb', line 40

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



34
35
36
# File 'lib/hbase-jruby/table.rb', line 34

def exists?
  with_admin { |admin| admin.tableExists @name }
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)


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

def has_coprocessor? class_name
  descriptor.hasCoprocessor(class_name)
end

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

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

Returns:

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


380
381
382
383
384
# File 'lib/hbase-jruby/table.rb', line 380

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

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

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



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/hbase-jruby/table.rb', line 351

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 table description

Returns:

  • (String)

    Table description



388
389
390
391
392
393
394
395
# File 'lib/hbase-jruby/table.rb', line 388

def inspect
  if exists?
    htable.get_table_descriptor.to_s
  else
    # FIXME
    "{NAME => '#{@name}'}"
  end
end

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

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



269
270
271
272
273
274
275
# File 'lib/hbase-jruby/table.rb', line 269

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

#remove_coprocessor!(name) ⇒ nil

Removes the coprocessor from the table.

Parameters:

  • class_name (String)

    Full class name of the coprocessor

Returns:

  • (nil)


194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/hbase-jruby/table.rb', line 194

def remove_coprocessor! name
  unless org.apache.hadoop.hbase.HTableDescriptor.respond_to?(:removeCoprocessor)
    raise NotImplementedError, "org.apache.hadoop.hbase.HTableDescriptor.removeCoprocessor not implemented"
  end
  with_admin do |admin|
    while_disabled(admin) do
      htd = admin.get_table_descriptor(@name.to_java_bytes)
      htd.removeCoprocessor name
      admin.modifyTable @name.to_java_bytes, htd
      wait_async_admin(admin)
    end
  end
end

#truncate!nil

Truncates the table by dropping it and recreating it.

Returns:

  • (nil)


233
234
235
236
237
# File 'lib/hbase-jruby/table.rb', line 233

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