diff --git hbase-shell/src/main/ruby/hbase/table.rb hbase-shell/src/main/ruby/hbase/table.rb index 3685eca..22d5d84 100644 --- hbase-shell/src/main/ruby/hbase/table.rb +++ hbase-shell/src/main/ruby/hbase/table.rb @@ -169,13 +169,38 @@ EOF #---------------------------------------------------------------------------------------------- # Increment a counter atomically - def _incr_internal(row, column, value = nil) + def _incr_internal(row, column, value = nil, args={}) + if value.kind_of?(Hash) + value = 1 + end value ||= 1 + incr = org.apache.hadoop.hbase.client.Increment.new(row.to_s.to_java_bytes) family, qualifier = parse_column_name(column) if qualifier.nil? - raise ArgumentError, "Failed to provide both column family and column qualifier for incr" + raise ArgumentError, "Failed to provide both column family and column qualifier for incr" + end + if args.any? + attributes = args[ATTRIBUTES] + set_attributes(incr, attributes) if attributes + end + incr.addColumn(family, qualifier, value) + @table.increment(incr) + end + + #---------------------------------------------------------------------------------------------- + # appends the value atomically + def _append_internal(row, column, value, args={}) + append = org.apache.hadoop.hbase.client.Append.new(row.to_s.to_java_bytes) + family, qualifier = parse_column_name(column) + if qualifier.nil? + raise ArgumentError, "Failed to provide both column family and column qualifier for append" + end + if args.any? + attributes = args[ATTRIBUTES] + set_attributes(append, attributes) if attributes end - @table.incrementColumnValue(row.to_s.to_java_bytes, family, qualifier, value) + append.add(family, qualifier, value.to_s.to_java_bytes) + @table.append(append) end #---------------------------------------------------------------------------------------------- diff --git hbase-shell/src/main/ruby/shell.rb hbase-shell/src/main/ruby/shell.rb index e48ce31..fef59b7 100644 --- hbase-shell/src/main/ruby/shell.rb +++ hbase-shell/src/main/ruby/shell.rb @@ -284,6 +284,7 @@ Shell.load_command_group( scan truncate truncate_preserve + append ] ) diff --git hbase-shell/src/main/ruby/shell/commands/append.rb hbase-shell/src/main/ruby/shell/commands/append.rb new file mode 100644 index 0000000..55da884 --- /dev/null +++ hbase-shell/src/main/ruby/shell/commands/append.rb @@ -0,0 +1,50 @@ +# +# +# 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 Append < Command + def help + return <<-EOF +Appends a cell 'value' at specified table/row/column coordinates. + + hbase> append 't1', 'r1', 'c1', 'value', ATTRIBUTES=>{'mykey'=>'myvalue'} + +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.append 'r1', 'c1', 'value', ATTRIBUTES=>{'mykey'=>'myvalue'} +EOF + end + + def command(table, row, column, value, args={}) + append(table(table), row, column, value, args) + end + + def append(table, row, column, value, args={}) + format_simple_command do + table._append_internal(row, column, value, args) + end + end + end + end +end + +#add incr comamnd to Table +::Hbase::Table.add_shell_command("append") diff --git hbase-shell/src/main/ruby/shell/commands/incr.rb hbase-shell/src/main/ruby/shell/commands/incr.rb index 6ebac29..c3c237e 100644 --- hbase-shell/src/main/ruby/shell/commands/incr.rb +++ hbase-shell/src/main/ruby/shell/commands/incr.rb @@ -29,23 +29,26 @@ To increment a cell value in table 't1' at row 'r1' under column hbase> incr 't1', 'r1', 'c1' hbase> incr 't1', 'r1', 'c1', 1 hbase> incr 't1', 'r1', 'c1', 10 + hbase> incr 't1', 'r1', 'c1', 10, ATTRIBUTES=>{'mykey'=>'myvalue'} + hbase> incr 't1', 'r1', 'c1', ATTRIBUTES=>{'mykey'=>'myvalue'} 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.incr 'r1', 'c1' hbase> t.incr 'r1', 'c1', 1 - hbase> t.incr 'r1', 'c1', 10 + hbase> t.incr 'r1', 'c1', 10, ATTRIBUTES=>{'mykey'=>'myvalue'} EOF end - def command(table, row, column, value) - incr(table(table), row, column, value) + def command(table, row, column, value = nil, args = {}) + incr(table(table), row, column, value, args) end - def incr(table, row, column, value = nil) - cnt = table._incr_internal(row, column, value) - puts "COUNTER VALUE = #{cnt}" + def incr(table, row, column, value = nil, args={}) + format_simple_command do + table._incr_internal(row, column, value, args) + end end end end diff --git hbase-shell/src/test/ruby/hbase/table_test.rb hbase-shell/src/test/ruby/hbase/table_test.rb index 0f63476..01781f8 100644 --- hbase-shell/src/test/ruby/hbase/table_test.rb +++ hbase-shell/src/test/ruby/hbase/table_test.rb @@ -187,6 +187,11 @@ module Hbase end #------------------------------------------------------------------------------- + + define_test "append should work with value" do + @test_table.append("123", 'x:cnt2', '123') + end + #------------------------------------------------------------------------------- define_test "get_counter should work with integer keys" do @test_table.incr(12345, 'x:cnt')