You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by st...@apache.org on 2010/03/13 21:32:11 UTC

svn commit: r922672 - in /hadoop/hbase/trunk: CHANGES.txt core/src/main/ruby/hbase/table.rb core/src/main/ruby/shell.rb core/src/main/ruby/shell/commands/get_counter.rb core/src/main/ruby/shell/commands/incr.rb core/src/test/ruby/hbase/table_test.rb

Author: stack
Date: Sat Mar 13 20:32:10 2010
New Revision: 922672

URL: http://svn.apache.org/viewvc?rev=922672&view=rev
Log:
HBASE-2314 [shell] Support for getting counters

Added:
    hadoop/hbase/trunk/core/src/main/ruby/shell/commands/get_counter.rb
Modified:
    hadoop/hbase/trunk/CHANGES.txt
    hadoop/hbase/trunk/core/src/main/ruby/hbase/table.rb
    hadoop/hbase/trunk/core/src/main/ruby/shell.rb
    hadoop/hbase/trunk/core/src/main/ruby/shell/commands/incr.rb
    hadoop/hbase/trunk/core/src/test/ruby/hbase/table_test.rb

Modified: hadoop/hbase/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/CHANGES.txt?rev=922672&r1=922671&r2=922672&view=diff
==============================================================================
--- hadoop/hbase/trunk/CHANGES.txt (original)
+++ hadoop/hbase/trunk/CHANGES.txt Sat Mar 13 20:32:10 2010
@@ -430,6 +430,7 @@ Release 0.21.0 - Unreleased
    HBASE-2309  Add apache releases to pom (list of ) repositories
                (Kay Kay via Stack)
    HBASE-2279  Hbase Shell does not have any tests (Alexey Kovyrin via Stack)
+   HBASE-2314  [shell] Support for getting counters (Alexey Kovyrin via Stack)
 
   NEW FEATURES
    HBASE-1961  HBase EC2 scripts

Modified: hadoop/hbase/trunk/core/src/main/ruby/hbase/table.rb
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/core/src/main/ruby/hbase/table.rb?rev=922672&r1=922671&r2=922672&view=diff
==============================================================================
--- hadoop/hbase/trunk/core/src/main/ruby/hbase/table.rb (original)
+++ hadoop/hbase/trunk/core/src/main/ruby/hbase/table.rb Sat Mar 13 20:32:10 2010
@@ -171,6 +171,24 @@ module Hbase
     end
 
     #----------------------------------------------------------------------------------------------
+    # Fetches and decodes a counter value from hbase
+    def get_counter(row, column)
+      family, qualifier = parse_column_name(column.to_s)
+      # Format get request
+      get = Get.new(row.to_s.to_java_bytes)
+      get.addColumn(family, qualifier)
+      get.setMaxVersions(1)
+
+      # Call hbase
+      result = @table.get(get)
+      return nil if result.isEmpty
+
+      # Fetch cell value
+      cell = result.list.first
+      Bytes::toLong(cell.getValue)
+    end
+
+    #----------------------------------------------------------------------------------------------
     # Scans whole table or a range of keys and returns rows matching specific criterias
     def scan(args = {})
       unless args.kind_of?(Hash)

Modified: hadoop/hbase/trunk/core/src/main/ruby/shell.rb
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/core/src/main/ruby/shell.rb?rev=922672&r1=922671&r2=922672&view=diff
==============================================================================
--- hadoop/hbase/trunk/core/src/main/ruby/shell.rb (original)
+++ hadoop/hbase/trunk/core/src/main/ruby/shell.rb Sat Mar 13 20:32:10 2010
@@ -211,6 +211,7 @@ Shell.load_command_group(
     delete
     deleteall
     get
+    get_counter
     incr
     put
     scan

Added: hadoop/hbase/trunk/core/src/main/ruby/shell/commands/get_counter.rb
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/core/src/main/ruby/shell/commands/get_counter.rb?rev=922672&view=auto
==============================================================================
--- hadoop/hbase/trunk/core/src/main/ruby/shell/commands/get_counter.rb (added)
+++ hadoop/hbase/trunk/core/src/main/ruby/shell/commands/get_counter.rb Sat Mar 13 20:32:10 2010
@@ -0,0 +1,23 @@
+module Shell
+  module Commands
+    class GetCounter < Command
+      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:
+
+            hbase> get_counter 't1', 'r1', 'c1'
+        EOF
+      end
+
+      def command(table, row, column, value = nil)
+        if cnt = table(table).get_counter(row, column)
+          puts "COUNTER VALUE = #{cnt}"
+        else
+          puts "No counter found at specified coordinates"
+        end
+      end
+    end
+  end
+end

Modified: hadoop/hbase/trunk/core/src/main/ruby/shell/commands/incr.rb
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/core/src/main/ruby/shell/commands/incr.rb?rev=922672&r1=922671&r2=922672&view=diff
==============================================================================
--- hadoop/hbase/trunk/core/src/main/ruby/shell/commands/incr.rb (original)
+++ hadoop/hbase/trunk/core/src/main/ruby/shell/commands/incr.rb Sat Mar 13 20:32:10 2010
@@ -14,9 +14,8 @@ module Shell
       end
 
       def command(table, row, column, value = nil)
-        format_simple_command do
-          table(table).incr(row, column, value)
-        end
+        cnt = table(table).incr(row, column, value)
+        puts "COUNTER VALUE = #{cnt}"
       end
     end
   end

Modified: hadoop/hbase/trunk/core/src/test/ruby/hbase/table_test.rb
URL: http://svn.apache.org/viewvc/hadoop/hbase/trunk/core/src/test/ruby/hbase/table_test.rb?rev=922672&r1=922671&r2=922672&view=diff
==============================================================================
--- hadoop/hbase/trunk/core/src/test/ruby/hbase/table_test.rb (original)
+++ hadoop/hbase/trunk/core/src/test/ruby/hbase/table_test.rb Sat Mar 13 20:32:10 2010
@@ -141,6 +141,17 @@ module Hbase
     define_test "incr should work with integer keys" do
       @test_table.incr(123, 'x:cnt3')
     end
+
+    #-------------------------------------------------------------------------------
+
+    define_test "get_counter should work with integer keys" do
+      @test_table.incr(12345, 'x:cnt')
+      assert_kind_of(Fixnum, @test_table.get_counter(12345, 'x:cnt'))
+    end
+
+    define_test "get_counter should return nil for non-existent counters" do
+      assert_nil(@test_table.get_counter(12345, 'x:qqqq'))
+    end
   end
 
   # Complex data management methods tests