Database Reference
In-Depth Information
hbase(main):005:0>get 'mycounters', 'Jan14'
COLUMN CELL
daily:hits timestamp=1408810501368,value=\x00\x00\x00\x00\x00\x00\x00\xAA
1 row(s) in 0.0140 seconds
The preceding output also proves that counters are treated as only columns.
However, the GET command provides the byte array representation as hexadecimal
values, whereas using get_counter shows the current value in a human-readable
format. Finally, counters are not only meant for incremental values but also for
decremental values as well or they can be omitted completely.
hbase(main):04:0>incr 'mycounters', 'Jan14', monthly:hits', -4
COUNTER VALUE = 146
The following Java code shows the handling of counters:
package com.ch5;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseCounterExample {
/**
* Example for incrementing counter value.
*/
Public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "mycounters");
table.incrementColumnValue(Bytes.toBytes("Jan14"), Bytes.
toBytes("monthly"),
Bytes.toBytes("hits"), 10L);
table.close();
}
}
The preceding Java program shows how we can increment the counter value using
the HBase API method, incrementColumnValue() .
 
Search WWH ::




Custom Search