From 867ff9fc69e6e0c21e2578749f3b270ce79efd01 Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Sun, 4 Nov 2012 21:09:02 +0000 Subject: [PATCH] HBASE-7089 Allow filter to be specified for Get from HBase shell git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1405640 13f79535-47bb-0310-9956-ffa450edef68 --- hbase-server/src/main/ruby/hbase/table.rb | 7 ++++ .../src/main/ruby/shell/commands/get.rb | 2 + .../src/test/ruby/hbase/table_test.rb | 38 ++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/ruby/hbase/table.rb b/hbase-server/src/main/ruby/hbase/table.rb index bcbb3ae69a0..64c0e102b64 100644 --- a/hbase-server/src/main/ruby/hbase/table.rb +++ b/hbase-server/src/main/ruby/hbase/table.rb @@ -210,6 +210,7 @@ EOF # Get maxlength parameter if passed maxlength = args.delete(MAXLENGTH) if args[MAXLENGTH] + filter = args.delete(FILTER) if args[FILTER] unless args.empty? columns = args[COLUMN] || args[COLUMNS] @@ -254,6 +255,12 @@ EOF end end + unless filter.class == String + get.setFilter(filter) + else + get.setFilter(org.apache.hadoop.hbase.filter.ParseFilter.new.parseFilterString(filter)) + end + # Call hbase for the results result = @table.get(get) return nil if result.isEmpty diff --git a/hbase-server/src/main/ruby/shell/commands/get.rb b/hbase-server/src/main/ruby/shell/commands/get.rb index 3dea6225047..4344d68d14f 100644 --- a/hbase-server/src/main/ruby/shell/commands/get.rb +++ b/hbase-server/src/main/ruby/shell/commands/get.rb @@ -32,6 +32,7 @@ a dictionary of column(s), timestamp, timerange and versions. Examples: hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1} hbase> get 't1', 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4} hbase> get 't1', 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4} + hbase> get 't1', 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"} hbase> get 't1', 'r1', 'c1' hbase> get 't1', 'r1', 'c1', 'c2' hbase> get 't1', 'r1', ['c1', 'c2'] @@ -61,6 +62,7 @@ would be: hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1} hbase> t.get 'r1', {COLUMN => 'c1', TIMERANGE => [ts1, ts2], VERSIONS => 4} hbase> t.get 'r1', {COLUMN => 'c1', TIMESTAMP => ts1, VERSIONS => 4} + hbase> t.get 'r1', {FILTER => "ValueFilter(=, 'binary:abc')"} hbase> t.get 'r1', 'c1' hbase> t.get 'r1', 'c1', 'c2' hbase> t.get 'r1', ['c1', 'c2'] diff --git a/hbase-server/src/test/ruby/hbase/table_test.rb b/hbase-server/src/test/ruby/hbase/table_test.rb index c11718872c7..8a1fbaa06af 100644 --- a/hbase-server/src/test/ruby/hbase/table_test.rb +++ b/hbase-server/src/test/ruby/hbase/table_test.rb @@ -328,6 +328,22 @@ module Hbase end end + define_test "get should support FILTER" do + @test_table.put(1, "x:v", "thisvalue") + begin + res = @test_table._get_internal('1', FILTER => "ValueFilter(=, 'binary:thisvalue')") + assert_not_nil(res) + assert_kind_of(Hash, res) + assert_not_nil(res['x:v']) + assert_nil(res['x:a']) + res = @test_table._get_internal('1', FILTER => "ValueFilter(=, 'binary:thatvalue')") + assert_nil(res) + ensure + # clean up newly added columns for this test only. + @test_table.delete(1, "x:v") + end + end + #------------------------------------------------------------------------------- define_test "scan should work w/o any params" do @@ -447,8 +463,26 @@ module Hbase # clean up newly added columns for this test only. @test_table.delete(1, "x:c") @test_table.delete(1, "x:d") + end end -end - + + define_test "scan should support FILTER" do + @test_table.put(1, "x:v", "thisvalue") + begin + res = @test_table._scan_internal FILTER => "ValueFilter(=, 'binary:thisvalue')" + assert_not_equal(res, {}, "Result is empty") + assert_kind_of(Hash, res) + assert_not_nil(res['1']) + assert_not_nil(res['1']['x:v']) + assert_nil(res['1']['x:a']) + assert_nil(res['2']) + res = @test_table._scan_internal FILTER => "ValueFilter(=, 'binary:thatvalue')" + assert_equal(res, {}, "Result is not empty") + ensure + # clean up newly added columns for this test only. + @test_table.delete(1, "x:v") + end + end + end end