You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hbase.apache.org by Ray Duong <ra...@gmail.com> on 2010/03/12 16:11:45 UTC

Hbase incr function

Hi Hbase,

I'm a little confuse on how the auto increment works in Hbase.  Shouldn't
the value be a number instead of a bytearray?  Is there a way to convert the
value into a string?


hbase(main):013:0> create 'testcounter', 'cf'
0 row(s) in 2.0950 seconds
3854.986: [GC 3854.987: [ParNew: 19136K->1671K(19136K), 0.0175270 secs]
27147K->11204K(83008K) icms_dc=7 , 0.0177320 secs] [Times: user=0.02
sys=0.03, real=0.01 secs]
hbase(main):014:0> scan 'testcounter'
ROW
COLUMN+CELL

0 row(s) in 0.0080 seconds
hbase(main):015:0> incr 'testcounter', 'r1', 'cf:counter', 1
0 row(s) in 0.0040 seconds
hbase(main):016:0> scan 'testcounter'
ROW
COLUMN+CELL

 r1                          column=cf:counter, timestamp=1268406376222,
value=\x00\x00\x00\x00\x00\x00\x00\x0

1

1 row(s) in 0.0110 seconds
hbase(main):017:0> incr 'testcounter', 'r1', 'cf:counter', 1
0 row(s) in 0.0030 seconds
hbase(main):018:0> scan 'testcounter'
ROW
COLUMN+CELL

 r1                          column=cf:counter, timestamp=1268406376222,
value=\x00\x00\x00\x00\x00\x00\x00\x0

2

1 row(s) in 0.0090 seconds
hbase(main):019:0> incr 'testcounter', 'r1', 'cf:counter', 1
0 row(s) in 0.0040 seconds
hbase(main):020:0> scan 'testcounter'
ROW
COLUMN+CELL

 r1                          column=cf:counter, timestamp=1268406376222,
value=\x00\x00\x00\x00\x00\x00\x00\x0

3

1 row(s) in 0.0080 seconds
hbase(main):021:0> incr 'testcounter', 'r1', 'cf:counter', 10
0 row(s) in 0.0040 seconds
hbase(main):022:0> scan 'testcounter'
ROW
COLUMN+CELL

 r1                          column=cf:counter, timestamp=1268406376222,
value=\x00\x00\x00\x00\x00\x00\x00\x0

D

1 row(s) in 0.0090 seconds
hbase(main):023:0> incr 'testcounter', 'r1', 'cf:counter', 100
0 row(s) in 0.0030 seconds
hbase(main):024:0> scan 'testcounter'
ROW
COLUMN+CELL

 r1                          column=cf:counter, timestamp=1268406376222,
value=\x00\x00\x00\x00\x00\x00\x00q
1 row(s) in 0.0230 seconds

Thanks,
-ray

Re: Hbase incr function

Posted by Ray Duong <ra...@gmail.com>.
Thanks Stack!

-ray

On Fri, Mar 12, 2010 at 7:42 AM, Stack <st...@duboce.net> wrote:

> Everything in hbase is a byte array.  Counters are converted from
> numbers to bytes when stored to hbase.
>
> Neither does hbase store column type information so whether the column
> value is a number or string is an application concern and opaque to
> hbase.
>
> What you are seeing below is the shell outputting the raw counter
> bytes.  As written it doesn't know any better regards the cell
> content.  What we should do is add to the shell a get_counter or some
> such method that will take the bytes from the specified cell value and
> emit them as a number (I made an issue to do this, HBASE-2314).  You
> the user know the cell contains a counter and would use the
> get_counter to fetch the current contents.
>
> Meantime, a bit of shell hackery should do the job for you.  Something
> like the following pseudo-code:
>
> hbase> t = HTable.new(@configuration, "TABLE_NAME")
> hbase> g = Get.new(Bytes.toBytes("ROW_STR")
> hbase> f = Bytes.toBytes("FAMILY")
> hbase> q = Bytes.toBytes("QUALFIER")
> hbase> g.addColumn(f, q)
> hbase> result = t.get(g)
> hbase> puts Bytes.toInteger(result.get(g, q))
>
> Make above into a method... etc.
>
> Good luck,
> St.Ack
>
> On Fri, Mar 12, 2010 at 7:11 AM, Ray Duong <ra...@gmail.com> wrote:
> > Hi Hbase,
> >
> > I'm a little confuse on how the auto increment works in Hbase.  Shouldn't
> > the value be a number instead of a bytearray?  Is there a way to convert
> the
> > value into a string?
> >
> >
> > hbase(main):013:0> create 'testcounter', 'cf'
> > 0 row(s) in 2.0950 seconds
> > 3854.986: [GC 3854.987: [ParNew: 19136K->1671K(19136K), 0.0175270 secs]
> > 27147K->11204K(83008K) icms_dc=7 , 0.0177320 secs] [Times: user=0.02
> > sys=0.03, real=0.01 secs]
> > hbase(main):014:0> scan 'testcounter'
> > ROW
> > COLUMN+CELL
> >
> > 0 row(s) in 0.0080 seconds
> > hbase(main):015:0> incr 'testcounter', 'r1', 'cf:counter', 1
> > 0 row(s) in 0.0040 seconds
> > hbase(main):016:0> scan 'testcounter'
> > ROW
> > COLUMN+CELL
> >
> >  r1                          column=cf:counter, timestamp=1268406376222,
> > value=\x00\x00\x00\x00\x00\x00\x00\x0
> >
> > 1
> >
> > 1 row(s) in 0.0110 seconds
> > hbase(main):017:0> incr 'testcounter', 'r1', 'cf:counter', 1
> > 0 row(s) in 0.0030 seconds
> > hbase(main):018:0> scan 'testcounter'
> > ROW
> > COLUMN+CELL
> >
> >  r1                          column=cf:counter, timestamp=1268406376222,
> > value=\x00\x00\x00\x00\x00\x00\x00\x0
> >
> > 2
> >
> > 1 row(s) in 0.0090 seconds
> > hbase(main):019:0> incr 'testcounter', 'r1', 'cf:counter', 1
> > 0 row(s) in 0.0040 seconds
> > hbase(main):020:0> scan 'testcounter'
> > ROW
> > COLUMN+CELL
> >
> >  r1                          column=cf:counter, timestamp=1268406376222,
> > value=\x00\x00\x00\x00\x00\x00\x00\x0
> >
> > 3
> >
> > 1 row(s) in 0.0080 seconds
> > hbase(main):021:0> incr 'testcounter', 'r1', 'cf:counter', 10
> > 0 row(s) in 0.0040 seconds
> > hbase(main):022:0> scan 'testcounter'
> > ROW
> > COLUMN+CELL
> >
> >  r1                          column=cf:counter, timestamp=1268406376222,
> > value=\x00\x00\x00\x00\x00\x00\x00\x0
> >
> > D
> >
> > 1 row(s) in 0.0090 seconds
> > hbase(main):023:0> incr 'testcounter', 'r1', 'cf:counter', 100
> > 0 row(s) in 0.0030 seconds
> > hbase(main):024:0> scan 'testcounter'
> > ROW
> > COLUMN+CELL
> >
> >  r1                          column=cf:counter, timestamp=1268406376222,
> > value=\x00\x00\x00\x00\x00\x00\x00q
> > 1 row(s) in 0.0230 seconds
> >
> > Thanks,
> > -ray
> >
>

Re: Hbase incr function

Posted by Stack <st...@duboce.net>.
I just applied this to TRUNK.
St.Ack

On Fri, Mar 12, 2010 at 10:13 PM, Alexey Kovyrin <al...@kovyrin.net> wrote:
> I've implemented get_counter shell command and improved incr command
> in HBASE-2314. If (when) the patch would be accepted, this
> functionality would be added to trunk shell. I'm not sure if we plan
> to backport those changes to 0.20 branch.
>
> On Fri, Mar 12, 2010 at 10:42 AM, Stack <st...@duboce.net> wrote:
>> Everything in hbase is a byte array.  Counters are converted from
>> numbers to bytes when stored to hbase.
>>
>> Neither does hbase store column type information so whether the column
>> value is a number or string is an application concern and opaque to
>> hbase.
>>
>> What you are seeing below is the shell outputting the raw counter
>> bytes.  As written it doesn't know any better regards the cell
>> content.  What we should do is add to the shell a get_counter or some
>> such method that will take the bytes from the specified cell value and
>> emit them as a number (I made an issue to do this, HBASE-2314).  You
>> the user know the cell contains a counter and would use the
>> get_counter to fetch the current contents.
>
> --
> Alexey Kovyrin
> http://kovyrin.net/
>

Re: Hbase incr function

Posted by Alexey Kovyrin <al...@kovyrin.net>.
I've implemented get_counter shell command and improved incr command
in HBASE-2314. If (when) the patch would be accepted, this
functionality would be added to trunk shell. I'm not sure if we plan
to backport those changes to 0.20 branch.

On Fri, Mar 12, 2010 at 10:42 AM, Stack <st...@duboce.net> wrote:
> Everything in hbase is a byte array.  Counters are converted from
> numbers to bytes when stored to hbase.
>
> Neither does hbase store column type information so whether the column
> value is a number or string is an application concern and opaque to
> hbase.
>
> What you are seeing below is the shell outputting the raw counter
> bytes.  As written it doesn't know any better regards the cell
> content.  What we should do is add to the shell a get_counter or some
> such method that will take the bytes from the specified cell value and
> emit them as a number (I made an issue to do this, HBASE-2314).  You
> the user know the cell contains a counter and would use the
> get_counter to fetch the current contents.

-- 
Alexey Kovyrin
http://kovyrin.net/

Re: Hbase incr function

Posted by Stack <st...@duboce.net>.
Everything in hbase is a byte array.  Counters are converted from
numbers to bytes when stored to hbase.

Neither does hbase store column type information so whether the column
value is a number or string is an application concern and opaque to
hbase.

What you are seeing below is the shell outputting the raw counter
bytes.  As written it doesn't know any better regards the cell
content.  What we should do is add to the shell a get_counter or some
such method that will take the bytes from the specified cell value and
emit them as a number (I made an issue to do this, HBASE-2314).  You
the user know the cell contains a counter and would use the
get_counter to fetch the current contents.

Meantime, a bit of shell hackery should do the job for you.  Something
like the following pseudo-code:

hbase> t = HTable.new(@configuration, "TABLE_NAME")
hbase> g = Get.new(Bytes.toBytes("ROW_STR")
hbase> f = Bytes.toBytes("FAMILY")
hbase> q = Bytes.toBytes("QUALFIER")
hbase> g.addColumn(f, q)
hbase> result = t.get(g)
hbase> puts Bytes.toInteger(result.get(g, q))

Make above into a method... etc.

Good luck,
St.Ack

On Fri, Mar 12, 2010 at 7:11 AM, Ray Duong <ra...@gmail.com> wrote:
> Hi Hbase,
>
> I'm a little confuse on how the auto increment works in Hbase.  Shouldn't
> the value be a number instead of a bytearray?  Is there a way to convert the
> value into a string?
>
>
> hbase(main):013:0> create 'testcounter', 'cf'
> 0 row(s) in 2.0950 seconds
> 3854.986: [GC 3854.987: [ParNew: 19136K->1671K(19136K), 0.0175270 secs]
> 27147K->11204K(83008K) icms_dc=7 , 0.0177320 secs] [Times: user=0.02
> sys=0.03, real=0.01 secs]
> hbase(main):014:0> scan 'testcounter'
> ROW
> COLUMN+CELL
>
> 0 row(s) in 0.0080 seconds
> hbase(main):015:0> incr 'testcounter', 'r1', 'cf:counter', 1
> 0 row(s) in 0.0040 seconds
> hbase(main):016:0> scan 'testcounter'
> ROW
> COLUMN+CELL
>
>  r1                          column=cf:counter, timestamp=1268406376222,
> value=\x00\x00\x00\x00\x00\x00\x00\x0
>
> 1
>
> 1 row(s) in 0.0110 seconds
> hbase(main):017:0> incr 'testcounter', 'r1', 'cf:counter', 1
> 0 row(s) in 0.0030 seconds
> hbase(main):018:0> scan 'testcounter'
> ROW
> COLUMN+CELL
>
>  r1                          column=cf:counter, timestamp=1268406376222,
> value=\x00\x00\x00\x00\x00\x00\x00\x0
>
> 2
>
> 1 row(s) in 0.0090 seconds
> hbase(main):019:0> incr 'testcounter', 'r1', 'cf:counter', 1
> 0 row(s) in 0.0040 seconds
> hbase(main):020:0> scan 'testcounter'
> ROW
> COLUMN+CELL
>
>  r1                          column=cf:counter, timestamp=1268406376222,
> value=\x00\x00\x00\x00\x00\x00\x00\x0
>
> 3
>
> 1 row(s) in 0.0080 seconds
> hbase(main):021:0> incr 'testcounter', 'r1', 'cf:counter', 10
> 0 row(s) in 0.0040 seconds
> hbase(main):022:0> scan 'testcounter'
> ROW
> COLUMN+CELL
>
>  r1                          column=cf:counter, timestamp=1268406376222,
> value=\x00\x00\x00\x00\x00\x00\x00\x0
>
> D
>
> 1 row(s) in 0.0090 seconds
> hbase(main):023:0> incr 'testcounter', 'r1', 'cf:counter', 100
> 0 row(s) in 0.0030 seconds
> hbase(main):024:0> scan 'testcounter'
> ROW
> COLUMN+CELL
>
>  r1                          column=cf:counter, timestamp=1268406376222,
> value=\x00\x00\x00\x00\x00\x00\x00q
> 1 row(s) in 0.0230 seconds
>
> Thanks,
> -ray
>