HBASE-10229-Support OperationAttributes in Increment and Append in Shell (Ram)
git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1553623 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d3451214f3
commit
9459b605b0
|
@ -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"
|
||||
end
|
||||
@table.incrementColumnValue(row.to_s.to_java_bytes, family, qualifier, value)
|
||||
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
|
||||
append.add(family, qualifier, value.to_s.to_java_bytes)
|
||||
@table.append(append)
|
||||
end
|
||||
|
||||
#----------------------------------------------------------------------------------------------
|
||||
|
|
|
@ -284,6 +284,7 @@ Shell.load_command_group(
|
|||
scan
|
||||
truncate
|
||||
truncate_preserve
|
||||
append
|
||||
]
|
||||
)
|
||||
|
||||
|
|
|
@ -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")
|
|
@ -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
|
||||
|
|
|
@ -188,6 +188,11 @@ module Hbase
|
|||
|
||||
#-------------------------------------------------------------------------------
|
||||
|
||||
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')
|
||||
assert_kind_of(Fixnum, @test_table._get_counter_internal(12345, 'x:cnt'))
|
||||
|
|
Loading…
Reference in New Issue