HBase之计数器

Posted similarface

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HBase之计数器相关的知识,希望对你有一定的参考价值。

 

HBase计数器
#创建counters表 列族[‘daily‘,‘weekly‘,‘monthly‘]
hbase(main):001:0> create ‘counters‘,‘daily‘,‘weekly‘,‘monthly‘
0 row(s) in 1.5670 seconds
=> Hbase::Table - counters

#递增命中 步长默认为1
hbase(main):002:0> incr ‘counters‘, ‘20150101‘, ‘daily:hits‘, 1
COUNTER VALUE = 1
0 row(s) in 0.3320 seconds

hbase(main):003:0> incr ‘counters‘, ‘20150101‘, ‘daily:hits‘, 1
COUNTER VALUE = 2
0 row(s) in 0.0140 seconds

#获取计数器
hbase(main):004:0> get_counter ‘counters‘, ‘20150101‘, ‘daily:hits‘
COUNTER VALUE = 2
#使用了put去修改计数器 会导致后面的错误 原因是‘1‘会转换成Bytes.toBytes()
hbase(main):020:0> put ‘counters‘ ,‘20150102‘,‘daily:hits‘,‘1‘
0 row(s) in 0.0520 seconds

hbase(main):021:0> incr ‘counters‘, ‘20150102‘, ‘daily:hits‘, 1

ERROR: org.apache.hadoop.hbase.DoNotRetryIOException: Field is not a long, it‘s 1 bytes wide
        at org.apache.hadoop.hbase.regionserver.HRegion.getLongValue(HRegion.java:7647)
        at org.apache.hadoop.hbase.regionserver.HRegion.applyIncrementsToColumnFamily(HRegion.java:7601)
        at org.apache.hadoop.hbase.regionserver.HRegion.doIncrement(HRegion.java:7480)
        at org.apache.hadoop.hbase.regionserver.HRegion.increment(HRegion.java:7440)
        at org.apache.hadoop.hbase.regionserver.RSRpcServices.increment(RSRpcServices.java:551)
        at org.apache.hadoop.hbase.regionserver.RSRpcServices.mutate(RSRpcServices.java:2227)
        at org.apache.hadoop.hbase.protobuf.generated.ClientProtos$ClientService$2.callBlockingMethod(ClientProtos.java:33646)
        at org.apache.hadoop.hbase.ipc.RpcServer.call(RpcServer.java:2178)
        at org.apache.hadoop.hbase.ipc.CallRunner.run(CallRunner.java:112)
        at org.apache.hadoop.hbase.ipc.RpcExecutor.consumerLoop(RpcExecutor.java:133)
        at org.apache.hadoop.hbase.ipc.RpcExecutor$1.run(RpcExecutor.java:108)
        at java.lang.Thread.run(Thread.java:745)

hbase(main):001:0> get ‘counters‘,‘20150102‘
COLUMN                                           CELL                                                                                                                                         
 daily:hits                                      timestamp=1472808748361, value=1                                                                                                             
1 row(s) in 0.3190 seconds

hbase(main):002:0> put ‘counters‘ ,‘20150102‘,‘daily:hits‘,‘1‘
0 row(s) in 0.0640 seconds

hbase(main):003:0> get ‘counters‘,‘20150102‘
COLUMN                                           CELL                                                                                                                                         
 daily:hits                                      timestamp=1472808858593, value=1                                                                                                             
1 row(s) in 0.0090 seconds
#计数步长20
hbase(main):004:0> incr ‘counters‘, ‘20150101‘, ‘daily:hits‘, 20
COUNTER VALUE = 22
0 row(s) in 0.0260 seconds

hbase(main):005:0> incr ‘counters‘, ‘20150101‘, ‘daily:hits‘, 20
COUNTER VALUE = 42
0 row(s) in 0.0090 seconds

#默认步长是1
hbase(main):009:0> 
hbase(main):010:0* incr ‘counters‘, ‘20150101‘, ‘daily:hits‘
COUNTER VALUE = 45
0 row(s) in 0.0100 seconds

#计数器可以-1
hbase(main):011:0> incr ‘counters‘, ‘20150101‘, ‘daily:hits‘, -1
COUNTER VALUE = 44
0 row(s) in 0.0110 seconds

hbase(main):012:0> incr ‘counters‘, ‘20150101‘, ‘daily:hits‘, -1
#计数也可为0
hbase(main):013:0> incr ‘counters‘, ‘20150101‘, ‘daily:hits‘, 0
COUNTER VALUE = 43
0 row(s) in 0.0080 seconds

 

以上是关于HBase之计数器的主要内容,如果未能解决你的问题,请参考以下文章

HBase 模式行键设计 - 增量计数器?

HBase过滤器,计数器和协处理器

HBase - 计数器 - 计数器的介绍以及使用 | 那伊抹微笑

HBase二次开发之搭建HBase调试环境,如何远程debug HBase源代码

为啥 HBase 计数操作这么慢

如何在 hbase 表中获取计数记录?查询记录的最快方法是啥?