Upsert in HBase using existing value

BadrF :

I'm using HBase to store some streaming data, and I have values that evolve from one insertion to another, and that use the existing value.

I have a table created like this :

hbase(main):005:0> create ‘mytable’,’mycolfam’

The table will contain one field, named val

First, I insert data as it comes (key=1, val=10) :

hbase(main):005:0> put 'mytable','1','mycolfam:val',10

And then, I have incoming data for the same key '1' (key=1, val=12), and the inserted value in HBase must be the sum of the new and the old value (10+12).

hbase(main):005:0> put 'mytable','1','mycolfam:val',oldvalue+newvalue

I tried the Get first to fetch the old value, but I want to do it in a more optimized way.

Anyone know how to do that ? Thanks.

sompet :

You can use counters and increment for this. It will executed in a single transaction unlike executing a get and a put to increment the value.

Note that the stored value is in binary format.

hbase(main):003:0> incr 'mytable', '1', 'mycolfam:val', 1
COUNTER VALUE = 1
Took 0.1761 seconds
hbase(main):004:0> get 'mytable', '1', 'mycolfam:val'
COLUMN                                    CELL
 mycolfam:val                             timestamp=1554226437325, value=\x00\x00\x00\x00\x00\x00\x00\x01
1 row(s)
Took 0.0379 seconds
hbase(main):005:0> incr 'mytable', '1', 'mycolfam:val', 10
COUNTER VALUE = 11
Took 0.0086 seconds
hbase(main):006:0> get 'mytable', '1', 'mycolfam:val'
COLUMN                                    CELL
 mycolfam:val                             timestamp=1554226478551, value=\x00\x00\x00\x00\x00\x00\x00\x0B
1 row(s)
Took 0.0207 seconds
hbase(main):007:0>

To subtract from the value just simply use negative number as the last parameter.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=150387&siteId=1