From 37e423aa399a6df937e89ee19ca0578962d855fe Mon Sep 17 00:00:00 2001 From: Lars George Date: Sat, 28 Mar 2015 10:46:23 +0100 Subject: [PATCH] HBASE-10728 get_counter value is never used. --- hbase-shell/src/main/ruby/hbase/table.rb | 31 +++++++++++++--------- .../src/main/ruby/shell/commands/get_counter.rb | 10 +++---- hbase-shell/src/main/ruby/shell/commands/incr.rb | 6 ++++- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/hbase-shell/src/main/ruby/hbase/table.rb b/hbase-shell/src/main/ruby/hbase/table.rb index b1d2671..1d32f3d 100644 --- a/hbase-shell/src/main/ruby/hbase/table.rb +++ b/hbase-shell/src/main/ruby/hbase/table.rb @@ -49,7 +49,7 @@ module Hbase end end end - + # General help for the table # class level so we can call it from anywhere def self.help @@ -103,7 +103,7 @@ Note that after dropping a table, your reference to it becomes useless and furth is undefined (and not recommended). EOF end - + #--------------------------------------------------------------------------------------------- # let external objects read the underlying table object @@ -151,7 +151,7 @@ EOF end end timestamp = nil - end + end if timestamp p.add(family, qualifier, timestamp, value.to_s.to_java_bytes) else @@ -162,14 +162,14 @@ EOF #---------------------------------------------------------------------------------------------- # Delete a cell - def _delete_internal(row, column, + def _delete_internal(row, column, timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args = {}) _deleteall_internal(row, column, timestamp, args) end #---------------------------------------------------------------------------------------------- # Delete a row - def _deleteall_internal(row, column = nil, + def _deleteall_internal(row, column = nil, timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args = {}) # delete operation doesn't need read permission. Retaining the read check for # meta table as a part of HBASE-5837. @@ -186,7 +186,7 @@ EOF if v.kind_of?(String) set_cell_visibility(d, v) if v end - end + end end if args.any? visibility = args[VISIBILITY] @@ -220,9 +220,14 @@ EOF set_op_ttl(incr, ttl) if ttl end incr.addColumn(family, qualifier, value) - @table.increment(incr) + result = @table.increment(incr) + return nil if result.isEmpty + + # Fetch cell value + cell = result.listCells[0] + org.apache.hadoop.hbase.util.Bytes::toLong(cell.getValue) end - + #---------------------------------------------------------------------------------------------- # appends the value atomically def _append_internal(row, column, value, args={}) @@ -263,7 +268,7 @@ EOF count += 1 next unless (block_given? && count % interval == 0) # Allow command modules to visualize counting process - yield(count, + yield(count, org.apache.hadoop.hbase.util.Bytes::toStringBinary(row.getRow)) end @@ -277,7 +282,7 @@ EOF get = org.apache.hadoop.hbase.client.Get.new(row.to_s.to_java_bytes) maxlength = -1 @converters.clear() - + # Normalize args args = args.first if args.first.kind_of?(Hash) if args.kind_of?(String) || args.kind_of?(Array) @@ -636,7 +641,7 @@ EOF end (maxlength != -1) ? val[0, maxlength] : val end - + def convert(column, kv) #use org.apache.hadoop.hbase.util.Bytes as the default class klazz_name = 'org.apache.hadoop.hbase.util.Bytes' @@ -648,7 +653,7 @@ EOF if matches.nil? # cannot match the pattern of 'c(className).functionname' # use the default klazz_name - converter = @converters[column] + converter = @converters[column] else klazz_name = matches[1] converter = matches[2] @@ -657,7 +662,7 @@ EOF method = eval(klazz_name).method(converter) return method.call(kv.getValue) # apply the converter end - + # if the column spec contains CONVERTER information, to get rid of :CONVERTER info from column pair. # 1. return back normal column pair as usual, i.e., "cf:qualifier[:CONVERTER]" to "cf" and "qualifier" only # 2. register the CONVERTER information based on column spec - "cf:qualifier" diff --git a/hbase-shell/src/main/ruby/shell/commands/get_counter.rb b/hbase-shell/src/main/ruby/shell/commands/get_counter.rb index 00cf64d..6708c6a 100644 --- a/hbase-shell/src/main/ruby/shell/commands/get_counter.rb +++ b/hbase-shell/src/main/ruby/shell/commands/get_counter.rb @@ -23,8 +23,8 @@ module Shell def help return <<-EOF Return a counter cell value at specified table/row/column coordinates. -A cell cell should be managed with atomic increment function oh HBase -and the data should be binary encoded. Example: +A counter cell should be managed with atomic increment functions on HBase +and the data should be binary encoded (as long value). Example: hbase> get_counter 'ns1:t1', 'r1', 'c1' hbase> get_counter 't1', 'r1', 'c1' @@ -36,11 +36,11 @@ t to table 't1', the corresponding command would be: EOF end - def command(table, row, column, value) - get_counter(table(table), row, column, value) + def command(table, row, column) + get_counter(table(table), row, column) end - def get_counter(table, row, column, value = nil) + def get_counter(table, row, column) if cnt = table._get_counter_internal(row, column) puts "COUNTER VALUE = #{cnt}" else diff --git a/hbase-shell/src/main/ruby/shell/commands/incr.rb b/hbase-shell/src/main/ruby/shell/commands/incr.rb index a59869c..d223a45 100644 --- a/hbase-shell/src/main/ruby/shell/commands/incr.rb +++ b/hbase-shell/src/main/ruby/shell/commands/incr.rb @@ -50,7 +50,11 @@ EOF def incr(table, row, column, value = nil, args={}) format_simple_command do - table._incr_internal(row, column, value, args) + if cnt = table._incr_internal(row, column, value, args) + puts "COUNTER VALUE = #{cnt}" + else + puts "No counter found at specified coordinates" + end end end end -- 2.1.3