Class: Rbase::Table
- Inherits:
-
Object
- Object
- Rbase::Table
- Defined in:
- lib/rbase/table.rb
Instance Method Summary collapse
- #each(batch_size = 100, columns = []) ⇒ Object
- #each_batch(batch_size = 100, columns = []) ⇒ Object
- #find(row) ⇒ Object
- #first(row) ⇒ Object (also: #[])
- #increment(row, family, column, value) ⇒ Object
-
#initialize(client, table_name) ⇒ Table
constructor
A new instance of Table.
- #insert(row, hash) ⇒ Object (also: #[]=)
- #multi_insert(rows) ⇒ Object
- #set_auto_flush(enabled) ⇒ Object
Constructor Details
#initialize(client, table_name) ⇒ Table
Returns a new instance of Table.
3 4 5 6 |
# File 'lib/rbase/table.rb', line 3 def initialize(client, table_name) @client = client @table_name = table_name.to_s end |
Instance Method Details
#each(batch_size = 100, columns = []) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/rbase/table.rb', line 50 def each(batch_size=100, columns=[]) each_batch(batch_size, columns) do |results| results.each do |result| yield(result) end end end |
#each_batch(batch_size = 100, columns = []) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rbase/table.rb', line 39 def each_batch(batch_size=100, columns=[]) columns = [columns] unless columns.is_a?(Array) scanner_id = @client.scannerOpen(@table_name, '', columns) loop do results = @client.scannerGetList(scanner_id, batch_size) break if results.length == 0 yield results.map {|r| row_to_hash(r) } end @client.scannerClose(scanner_id) end |
#find(row) ⇒ Object
8 9 10 |
# File 'lib/rbase/table.rb', line 8 def find(row) @client.getRow(@table_name,row).map { |row| row_to_hash(row) } end |
#first(row) ⇒ Object Also known as: []
12 13 14 |
# File 'lib/rbase/table.rb', line 12 def first(row) find(row).first end |
#increment(row, family, column, value) ⇒ Object
62 63 64 |
# File 'lib/rbase/table.rb', line 62 def increment(row, family, column, value) @client.atomicIncrement(@table_name, row, "#{family}:#{column}", value) end |
#insert(row, hash) ⇒ Object Also known as: []=
17 18 19 20 21 22 23 24 |
# File 'lib/rbase/table.rb', line 17 def insert(row, hash) hash.each do |family,value| mutations = value.map do |column,val| Apache::Hadoop::Hbase::Thrift::Mutation.new(:column => "#{family}:#{column}", :value => val) end @client.mutateRow(@table_name,row,mutations) end end |
#multi_insert(rows) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rbase/table.rb', line 27 def multi_insert(rows) row_mutations = rows.map do |row, hash| hash.map do |family,value| mutations = value.map do |column,val| Apache::Hadoop::Hbase::Thrift::Mutation.new(:column => "#{family}:#{column}", :value => val) end Apache::Hadoop::Hbase::Thrift::BatchMutation.new(:row => row, :mutations => mutations) end end @client.mutateRows(@table_name, row_mutations.flatten) end |
#set_auto_flush(enabled) ⇒ Object
58 59 60 |
# File 'lib/rbase/table.rb', line 58 def set_auto_flush(enabled) @client.setAutoFlush(@table_name, enabled) end |