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

JAVA操作:

单计数器:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; /**
* 单计数器
* similarface
* similarface@outlook.com
*/
public class SingleCounter {
public static void main(String args[]) throws IOException{
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf("counters"));
//incrementColumnValue(行号,列族,列,步长)
long cnt1=table.incrementColumnValue(Bytes.toBytes("20150105"),Bytes.toBytes("daily"),Bytes.toBytes("hits"),1L);
System.out.println(cnt1);
long cnt2=table.incrementColumnValue(Bytes.toBytes("20150105"),Bytes.toBytes("daily"),Bytes.toBytes("hits"),1);
System.out.println(cnt2);
long current=table.incrementColumnValue(Bytes.toBytes("20150105"),Bytes.toBytes("daily"),Bytes.toBytes("hits"),0);
System.out.println(current);
long cnt3=table.incrementColumnValue(Bytes.toBytes("20150105"),Bytes.toBytes("daily"),Bytes.toBytes("hits"),-1);
System.out.println(cnt3);
table.close();
connection.close();
}
}
/**
1
2
2
1
**/

复合计数器:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException; /**
* 复合计数器
* similarface
* similarface@outlook.com
*/
public class MultipleCounter {
public static void main(String args[]) throws IOException{
Configuration configuration = HBaseConfiguration.create();
Connection connection = ConnectionFactory.createConnection(configuration);
Table table = connection.getTable(TableName.valueOf("counters"));
Increment increment1 = new Increment(Bytes.toBytes("20160101"));
increment1.addColumn(Bytes.toBytes("daily"),Bytes.toBytes("clicks"),1);
increment1.addColumn(Bytes.toBytes("daily"),Bytes.toBytes("hits"),1);
increment1.addColumn(Bytes.toBytes("weekly"),Bytes.toBytes("clicks"),10);
increment1.addColumn(Bytes.toBytes("weekly"),Bytes.toBytes("hits"),10); Result result = table.increment(increment1);
for(Cell cell:result.rawCells()){
System.out.println("Cell: " + cell +
" Value: " + Bytes.toLong(cell.getValueArray(), cell.getValueOffset(),cell.getValueLength()));
} Increment increment2 = new Increment(Bytes.toBytes("20160101"));
increment2.addColumn(Bytes.toBytes("daily"),Bytes.toBytes("clicks"), 5);
increment2.addColumn(Bytes.toBytes("daily"),Bytes.toBytes("hits"), 1);
increment2.addColumn(Bytes.toBytes("weekly"),Bytes.toBytes("clicks"), 0);
increment2.addColumn(Bytes.toBytes("weekly"),Bytes.toBytes("hits"), -5);
Result result2 = table.increment(increment2);
for (Cell cell : result2.rawCells()) {
System.out.println("Cell: " + cell +
" Value: " + Bytes.toLong(cell.getValueArray(),
cell.getValueOffset(), cell.getValueLength()));
} table.close();
connection.close();
}
}
/**
Cell: 20160101/daily:clicks/1473057324875/Put/vlen=8/seqid=0 Value: 1
Cell: 20160101/daily:hits/1473057324875/Put/vlen=8/seqid=0 Value: 1
Cell: 20160101/weekly:clicks/1473057324875/Put/vlen=8/seqid=0 Value: 10
Cell: 20160101/weekly:hits/1473057324875/Put/vlen=8/seqid=0 Value: 10
Cell: 20160101/daily:clicks/1473057324886/Put/vlen=8/seqid=0 Value: 6
Cell: 20160101/daily:hits/1473057324886/Put/vlen=8/seqid=0 Value: 2
Cell: 20160101/weekly:clicks/1473057324886/Put/vlen=8/seqid=0 Value: 10
Cell: 20160101/weekly:hits/1473057324886/Put/vlen=8/seqid=0 Value: 5
**/

  

  

HBase之计数器的更多相关文章

  1. HBase使用场景和成功案例 (转)

    HBase 使用场景和成功案例 有时候了解软件产品的最好方法是看看它是怎么用的.它可以解决什么问题和这些解决方案如何适用于大型应用架构,能够告诉你很多.因为HBase有许多公开的产品部署,我们正好可以 ...

  2. 8.HBase In Action 第一章-HBase简介(1.2.2 捕获增量数据)

    Data often trickles in and is added to an existing data store for further usage, such as analytics, ...

  3. HBase使用场景和成功案例

    1 典型互联网搜索问题:BigTable发明的原因 搜索使用场景 1) 爬虫持续不断地抓取新页面,这些页面每页一行地存储到HBase里. 2 )MapReduce计算作业运行在整张表上,生成索引,为网 ...

  4. HBase 使用场景和成功案例

    有时候了解软件产品的最好方法是看看它是怎么用的.它可以解决什么问题和这些解决方案如何适用于大型应用架构,能够告诉你很多.因为HBase有许多公开的产品部署,我们正好可以这么做.本章节将详细介绍一些人们 ...

  5. Storm+HBase实时实践

    1.HBase Increment计数器 hbase counter的原理: read+count+write,正好完成,就是讲key的value读出,若存在,则完成累加,再写入,若不存在,则按&qu ...

  6. hbase之认识

    进入HBase客户端命令操作界面    $ bin/hbase shell 查看帮助命令        hbase(main):001:0> help 查看当前数据库中有哪些表        h ...

  7. HBase操作(Shell与Java API)

    版权声明:本文为博主原创文章,未经博主允许不得转载.     转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...

  8. 通过HBase API进行开发

    http://www.cnblogs.com/netbloomy/p/6683509.html 一.将HBase的jar包及hbase-site.xml添加到IDE 1.到安装HBase集群的任意一台 ...

  9. 2、通过HBase API进行开发

    一.将HBase的jar包及hbase-site.xml添加到IDE 1.到安装HBase集群的任意一台机器上找到HBase的安装目录,到lib目录下下载HBase需要的jar包,然后再到conf目录 ...

随机推荐

  1. 使用Notepad++代替笨拙的Arduino IDE

    Arduino自带的IDE 太不好用了,下面是用强大,轻量级,免费,开源,丰富插件的的Notepad++编辑器来打造Arduino开发环境 . 配置过程对新生来说可能有些繁琐,但是我尽力写的很详细了, ...

  2. sql 在not in 子查询有null值情况下经常出现的陷阱

    如果下:TempSalesPriceFixedValues表和SalesPriceFixedValues表,要求查询出在TempSalesPriceFixedValues表中且不在SalesPrice ...

  3. Synchronized 个人深解

      1.synchronized方法相当于synchronized(this)      Synchronized 方法是锁的当前对象,同一个对象的2个synchronized方法被2个线程调用会发生 ...

  4. for循环求交集

    for循环方式求交集 #!/usr/bin/env python #coding:utf-8 #取交集 #定义两个序列对象,求alist与blist对象的交集元素 alist = [2,9,3,4,5 ...

  5. SQL Server ->> EXECUTE AS LOGIN/USER和Revert表达式

    EXECUTE AS LOGIN/USER和Revert表达式都是从SQL Server 2005就有.Revert的作用是用于切换当前过程的执行上下文返回上一个EXECUTE AS 语句发生之前的安 ...

  6. insert table 和create table as 区别

    首先,最大的区别是二者属于不同类型的语句,前者是DML语句(数据操作语言,SQL中处理数据等操作统称为数据操纵语言),完成后需要提交才能生效,后者是DDL语句(数据定义语言,用于定义和管理 SQL 数 ...

  7. Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)

    控制台程序. 在Sketcher中创建形状时,并不知道应该以什么顺序创建不同类型的形状,这完全取决于使用Sketcher程序生成草图的人.因此需要绘制形状,对它们执行其他操作而不必知道图形是什么.当然 ...

  8. Struts 404 The requested resource is not available

    出现这种错误一般是struts.xml配置错误,重点针对<action> 的class属性的全名 因为struts感觉应该是先加载所有的配置文件,如果配置文件有错误的话,所有的资源就都无效 ...

  9. csuoj 1503: 点到圆弧的距离

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1503 1503: 点到圆弧的距离 时间限制: 1 Sec  内存限制: 128 MB  Speci ...

  10. 一种比较少见的C#代码段

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...