Hbase provides us a mechanism to treat columns as counters. Counters allow us to increment a column value with least possible overhead.
Advantage of using Counters
While trying to increment a value stored in a table, we would have to lock the row, read the value, increment it, write it back to the table, and finally remove the look from the row, so that it can be used by other clients. This could cause a row to be locked for a long period and may possibly cause a clash between the clients tying to access the same row. Counters help us to overcome this problem as Increments are done under a single row lock, so write operations to a row are synchronized.
**Older versions of Hbase supported calls which involved one RPC per counter update. But the newer versions allow us to bundle multiple counters in a single RPC call.
Counters are limited to a single row, though we can update multiple counters simultaneously. This means that we operate on one row at a time when working with counters.
Hbase API provide the Increment class to perform Increment operations.
**To increment columns of a row, instantiate an Increment object with the row to increment. At least one column to increment must be specified using the
addColumn(byte[], byte[], long)
method.Alternatively we can use the the incrementColumnValue(row, family, qualifier, amount) method through an instance of Htable class.
Here is a quick example :
First create a demo table
hbase(main):060:0> create 'demo', 'cf'
0 row(s) in 1.4960 seconds
package demo.increment.hbase;
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.client.Increment;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseIncrementDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
Configuration conf = HBaseConfiguration.create();
HTable table = new HTable(conf, "demo");
table.incrementColumnValue(Bytes.toBytes("row1"), Bytes.toBytes("cf"), Bytes.toBytes("col"), 10L);
}
}
This piece of code will lock the row, row1 and increment the value of the column col by 10.
Now to check whether our operation was successful or not, we will use the get_counter shell cmmand. If everything was normal we will see something like his on our Hbase shell.
hbase(main):062:0> get_counter 'demo', 'row1', 'cf:col'
COUNTER VALUE = 10
**We
should not initialize the counters, as they are automatically assumed to be zero when we first use a new counter. This means that a column qualifier that does not yet exist. The first increment call to a new counter will return the increment value specified by us as its result.**The increment value must be a long value.Types of counters
Hbase provides two types of counters :
1 - Single Counters
2 - Multiple Counters
In the next part of this writeup i'll cover both these types. Till then KEEP HADOOPING.
NOTE : PLEASE DO NOT FORGET TO PROVIDE ME YOUR VALUABLE COMMENTS AND SUGGESTIONS.
nice post tariq
ReplyDeletethanks for the greate topic
ReplyDelete