Class: HBase::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/hbase/table.rb

Constant Summary collapse

TRANSLATION =
{
  :start => :start_row,
  :end => :end_row
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stargate, table_name) ⇒ Table

Returns a new instance of Table.



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

def initialize(stargate, table_name)
  @stargate, @table_name = stargate, table_name
end

Instance Attribute Details

#stargateObject (readonly)

Returns the value of attribute stargate.



3
4
5
# File 'lib/hbase/table.rb', line 3

def stargate
  @stargate
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



3
4
5
# File 'lib/hbase/table.rb', line 3

def table_name
  @table_name
end

Instance Method Details

#delete(key) ⇒ Object



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

def delete(key)
  stargate.delete_row table_name, key      
end

#get(key, with_timestamps = false) ⇒ Object Also known as: []



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

def get(key, with_timestamps = false)
  begin
    record = Record.new
    record.id = key        
    old_row = stargate.show_row(table_name, key)
    old_row.columns.each do |old_cell|
      record.cells[old_cell.name] = Cell.new old_cell.value, old_cell.timestamp
    end
    record
  rescue Stargate::RowNotFoundError
    nil
  end
end

#include?(key) ⇒ Boolean Also known as: exist?

Returns:

  • (Boolean)


38
39
40
# File 'lib/hbase/table.rb', line 38

def include?(key)
  !!get(key)      
end

#metadataObject



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

def 
  stargate.show_table table_name
end

#scan(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hbase/table.rb', line 51

def scan(options = {})
  options.symbolize_keys
  
  stargate_options = {}
  options.each do |k, v|
    stargate_options[TRANSLATION[k] || k] = v
  end
  
  scanner = stargate.open_scanner(table_name, stargate_options)
  begin
    old_rows = stargate.get_rows(scanner)        
    records = []
    old_rows.each do |old_row|          
      record = Record.new
      record.id = old_row.name
      old_row.columns.each do |old_cell|                        
        record.cells[old_cell.name] = Cell.new old_cell.value, old_cell.timestamp            
      end          
      records << record
    end
    records
  ensure
    stargate.close_scanner(scanner) if scanner
  end
end

#update(key, attributes) ⇒ Object Also known as: []=



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

def update(key, attributes)
  raise "ivalid usage, attributes should be a hash!" unless attributes.is_a? Hash
  timestamp = attributes.delete(:timestamp) || attributes.delete('timestamp') # || Time.now.to_i      
  attr_in_driver_format = attributes.to_a.collect do |attr_name, attr_value| 
    {:name => attr_name, :value => attr_value}
  end      
  stargate.create_row(table_name, key, timestamp, attr_in_driver_format)
end