Repository: hbase Updated Branches: refs/heads/master 8ef6c7634 -> 1d6c90b49
HBASE-16381 Shell deleteall command should support row key prefixes (Yi Liang) Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/1d6c90b4 Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/1d6c90b4 Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/1d6c90b4 Branch: refs/heads/master Commit: 1d6c90b4969b8ec47699c69984be052050a9ee46 Parents: 8ef6c76 Author: chenheng <chenh...@apache.org> Authored: Thu Sep 15 19:18:47 2016 +0800 Committer: chenheng <chenh...@apache.org> Committed: Thu Sep 15 19:20:29 2016 +0800 ---------------------------------------------------------------------- hbase-shell/src/main/ruby/hbase/table.rb | 81 +++++++++++++++----- .../src/main/ruby/shell/commands/deleteall.rb | 17 +++- hbase-shell/src/test/ruby/hbase/table_test.rb | 12 +++ 3 files changed, 86 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/1d6c90b4/hbase-shell/src/main/ruby/hbase/table.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/main/ruby/hbase/table.rb b/hbase-shell/src/main/ruby/hbase/table.rb index 5930c0d..22bbcfe 100644 --- a/hbase-shell/src/main/ruby/hbase/table.rb +++ b/hbase-shell/src/main/ruby/hbase/table.rb @@ -160,6 +160,62 @@ EOF end #---------------------------------------------------------------------------------------------- + # Create a Delete mutation + def _createdelete_internal(row, column = nil, + timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args = {}) + temptimestamp = timestamp + if temptimestamp.kind_of?(Hash) + timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP + end + d = org.apache.hadoop.hbase.client.Delete.new(row.to_s.to_java_bytes, timestamp) + if temptimestamp.kind_of?(Hash) + temptimestamp.each do |k, v| + if v.kind_of?(String) + set_cell_visibility(d, v) if v + end + end + end + if args.any? + visibility = args[VISIBILITY] + set_cell_visibility(d, visibility) if visibility + end + if column + family, qualifier = parse_column_name(column) + d.addColumns(family, qualifier, timestamp) + end + return d + end + + #---------------------------------------------------------------------------------------------- + # Delete rows using prefix + def _deleterows_internal(row, column = nil, + timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args={}) + cache = row["CACHE"] ? row["CACHE"] : 100 + prefix = row["ROWPREFIXFILTER"] + + # create scan to get table names using prefix + scan = org.apache.hadoop.hbase.client.Scan.new + scan.setRowPrefixFilter(prefix.to_java_bytes) + # Run the scanner to get all rowkeys + scanner = @table.getScanner(scan) + # Create a list to store all deletes + list = java.util.ArrayList.new + # Iterate results + iter = scanner.iterator + while iter.hasNext + row = iter.next + key = org.apache.hadoop.hbase.util.Bytes::toStringBinary(row.getRow) + d = _createdelete_internal(key, column, timestamp, args) + list.add(d) + if list.size >= cache + @table.delete(list) + list.clear + end + end + @table.delete(list) + end + + #---------------------------------------------------------------------------------------------- # Delete a cell def _delete_internal(row, column, timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args = {}) @@ -175,27 +231,12 @@ EOF if is_meta_table? raise ArgumentError, "Row Not Found" if _get_internal(row).nil? end - temptimestamp = timestamp - if temptimestamp.kind_of?(Hash) - timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP - end - d = org.apache.hadoop.hbase.client.Delete.new(row.to_s.to_java_bytes, timestamp) - if temptimestamp.kind_of?(Hash) - temptimestamp.each do |k, v| - if v.kind_of?(String) - set_cell_visibility(d, v) if v - end - end - end - if args.any? - visibility = args[VISIBILITY] - set_cell_visibility(d, visibility) if visibility - end - if column - family, qualifier = parse_column_name(column) - d.addColumns(family, qualifier, timestamp) + if row.kind_of?(Hash) + _deleterows_internal(row, column, timestamp, args) + else + d = _createdelete_internal(row, column, timestamp, args) + @table.delete(d) end - @table.delete(d) end #---------------------------------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/1d6c90b4/hbase-shell/src/main/ruby/shell/commands/deleteall.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/main/ruby/shell/commands/deleteall.rb b/hbase-shell/src/main/ruby/shell/commands/deleteall.rb index 2965403..033ec67 100644 --- a/hbase-shell/src/main/ruby/shell/commands/deleteall.rb +++ b/hbase-shell/src/main/ruby/shell/commands/deleteall.rb @@ -23,7 +23,8 @@ module Shell def help return <<-EOF Delete all cells in a given row; pass a table name, row, and optionally -a column and timestamp. Examples: +a column and timestamp. Deleteall also support deleting a row range using a +row key prefix. Examples: hbase> deleteall 'ns1:t1', 'r1' hbase> deleteall 't1', 'r1' @@ -31,13 +32,21 @@ a column and timestamp. Examples: hbase> deleteall 't1', 'r1', 'c1', ts1 hbase> deleteall 't1', 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'} +ROWPREFIXFILTER can be used to delete row ranges + hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'} + hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'}, 'c1' //delete certain column family in the row ranges + hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'}, 'c1', ts1 + hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix'}, 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'} + +CACHE can be used to specify how many deletes batched to be sent to server at one time, default is 100 + hbase> deleteall 't1', {ROWPREFIXFILTER => 'prefix', CACHE => 100} + + The same commands also can be run on a table reference. Suppose you had a reference t to table 't1', the corresponding command would be: - hbase> t.deleteall 'r1' - hbase> t.deleteall 'r1', 'c1' - hbase> t.deleteall 'r1', 'c1', ts1 hbase> t.deleteall 'r1', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'} + hbase> t.deleteall {ROWPREFIXFILTER => 'prefix', CACHE => 100}, 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'} EOF end http://git-wip-us.apache.org/repos/asf/hbase/blob/1d6c90b4/hbase-shell/src/test/ruby/hbase/table_test.rb ---------------------------------------------------------------------- diff --git a/hbase-shell/src/test/ruby/hbase/table_test.rb b/hbase-shell/src/test/ruby/hbase/table_test.rb index faf9827..53d0ca9 100644 --- a/hbase-shell/src/test/ruby/hbase/table_test.rb +++ b/hbase-shell/src/test/ruby/hbase/table_test.rb @@ -120,6 +120,10 @@ module Hbase @test_table.put(105, "x:a", "3") @test_table.put(105, "x:a", "4") + + @test_table.put("111", "x:a", "5") + @test_table.put("111", "x:b", "6") + @test_table.put("112", "x:a", "5") end def teardown @@ -181,6 +185,14 @@ module Hbase assert_nil(res) end + define_test "deletall should work with row prefix" do + @test_table.deleteall({ROWPREFIXFILTER => '11'}) + res1 = @test_table._get_internal('111') + assert_nil(res1) + res2 = @test_table._get_internal('112') + assert_nil(res2) + end + define_test "append should work with value" do @test_table.append("123", 'x:cnt2', '123') end