Database Reference
In-Depth Information
The older version of HBase supports only a single counter update
per RPC call but in newer versions, the client API supports updating
multiple counters per RPC call. In the case of multiple counters as
well, the RPC call is limited to a single row only.
Let's see the counter's behavior at the column level. The following code is the
format to increment the column value as a counter using HBase shells for the
incr
command:
incr '<table>', '<row>', '<column>', [<increment-value>]
The following commands show the creation of a table,
'mycounters'
, and then
increments a column,
monthly:hits
, value twice and the last value in the last query:
hbase(main):001:0> create 'mycounters', 'monthly'
0 row(s) in 3.1950 seconds
hbase(main):002:0>incr 'mycounters', 'Jan14', monthly:hits', 50
COUNTER VALUE = 50
hbase(main):003:0>incr 'mycounters', 'Jan14', monthly:hits', 100
COUNTER VALUE = 150
hbase(main):04:0>get_counter 'mycounters', 'Jan14', monthly:hits'
COUNTER VALUE = 150
Each call to
incr
returns the incremented value of the
monthly:hits
counter
and inally checks the current value of the
monthly:hits
counter using the
get_counter
command.
Note that counters can be incremented by one or any other value.
The default value of the counters is
zero
; hence, counters are not initialized at the
time of creation. In the previous chapter, it was discussed that the
PUT
command is
used to change the value for any column in HBase, but as a matter of precaution, it
is mandatory to use the
incr
command to increment the counter type column value.
The
PUT
command stores the value in a different format, which gives an erroneous
value while accessing. We can also use the
GET
command in place of
get_counter
to access the counter value as follows: