diff --git a/hbase-shell/src/main/ruby/hbase/table.rb b/hbase-shell/src/main/ruby/hbase/table.rb index 5930c0d..59b9e67 100644 --- a/hbase-shell/src/main/ruby/hbase/table.rb +++ b/hbase-shell/src/main/ruby/hbase/table.rb @@ -199,6 +199,55 @@ EOF end #---------------------------------------------------------------------------------------------- + # Delete rows using prefix + def _deleterows_internal(prefix, column = nil, + timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args={}) + # 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) + end + @table.delete(list) + 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 + + #---------------------------------------------------------------------------------------------- # Increment a counter atomically def _incr_internal(row, column, value = nil, args={}) if value.kind_of?(Hash) diff --git a/hbase-shell/src/main/ruby/shell.rb b/hbase-shell/src/main/ruby/shell.rb index bb6a604..9c52c7b 100644 --- a/hbase-shell/src/main/ruby/shell.rb +++ b/hbase-shell/src/main/ruby/shell.rb @@ -311,6 +311,7 @@ Shell.load_command_group( count delete deleteall + deleterows get get_counter incr diff --git a/hbase-shell/src/main/ruby/shell/commands/deleterows.rb b/hbase-shell/src/main/ruby/shell/commands/deleterows.rb new file mode 100644 index 0000000..bec0f8b --- /dev/null +++ b/hbase-shell/src/main/ruby/shell/commands/deleterows.rb @@ -0,0 +1,57 @@ +# +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +module Shell + module Commands + class Deleterows < Command + def help + return <<-EOF +Delete a row range using a rowkey prefix; pass a table name, rowkey prefix, and optionally +a column and timestamp. Examples: + + hbase> deleterows 'ns1:t1', 'prefix' + hbase> deleterows 't1', 'prefix' + hbase> deleterows 't1', 'prefix', 'c1' + hbase> deleterows 't1', 'prefix', 'c1', ts1 + hbase> deleterows 't1', 'prefix', 'c1', {VISIBILITY=>'PRIVATE|SECRET'} + hbase> deleterows 't1', 'prefix', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'} + +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.deleterows 'prefix', 'c1', ts1, {VISIBILITY=>'PRIVATE|SECRET'} +EOF + end + + def command(table, row, column = nil, + timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args = {}) + deleterows(table(table), row, column, timestamp, args) + end + + def deleterows(table, row, column = nil, + timestamp = org.apache.hadoop.hbase.HConstants::LATEST_TIMESTAMP, args = {}) + @start_time = Time.now + table._deleterows_internal(row, column, timestamp, args) + end + end + end +end + +# Add the method table.deleteall that calls deleterows.deleterows +::Hbase::Table.add_shell_command("deleterows")