Hbase 计数器

Hbase计数器可以用于统计用户数,点击量等信息

 

基本操作


 

 

可以使用incr操作计数器,incr语法格式如下:

incr '<table>', '<row>', '<column>', |<increment-value>|

然后使用get_counter可以获取对应的计数器的值

不用初始化计数器,第一次使用计数器时,计数器被自动设置为0

 

eg:

对于wishTest1表 

incr 'wishTest1','2','score:Math',1

incr 'wishTest1','2','score:Math',1

get_counter 'wishTest1','2','score:Math'

结果如下:

复制代码
hbase(main):004:0> incr 'wishTest1','2','score:Math',1
COUNTER VALUE = 1

hbase(main):005:0> incr 'wishTest1','2','score:Math',1
COUNTER VALUE = 2


hbase(main):007:0> get_counter 'wishTest1','2','score:Math'
COUNTER VALUE = 2

hbase(main):008:0>
复制代码

 

增大计数的递增值:

复制代码
hbase(main):001:0> incr 'wishTest1','2','score:Math',10
COUNTER VALUE = 12

hbase(main):002:0> incr 'wishTest1','2','score:Math',10
COUNTER VALUE = 22

hbase(main):003:0> get_counter 'wishTest1','2','score:Math'
COUNTER VALUE = 22
复制代码

 

使用API操作计数器


 

 

使用table.incrementColumnValue()

eg:

复制代码
     String tableName = "wishTest1";
        HTablePool pool = new HTablePool(cfg, 1000);
        try {
            long count1 = pool.getTable(tableName).incrementColumnValue(Bytes.toBytes("2"),Bytes.toBytes("score"),Bytes.toBytes("Math"),1);
            long count2 = pool.getTable(tableName).incrementColumnValue(Bytes.toBytes("2"),Bytes.toBytes("score"),Bytes.toBytes("Math"),1);
            long currentCount = pool.getTable(tableName).incrementColumnValue(Bytes.toBytes("2"),Bytes.toBytes("score"),Bytes.toBytes("Math"),0);
            System.out.println("count1: "+count1);
            System.out.println("count2: "+count2);
            System.out.println("currentCount: "+currentCount);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
复制代码

输出:

count1: 23
count2: 24
currentCount: 24

 

posted @ 2014-05-29 18:41  wishyouhappy  阅读(3709)  评论(0编辑  收藏  举报