//创建一个事例表
create 'demo','example'
//初始化计算器,当用户第一次使用计数器时,计数器将自动设为0
incr 'demo','1','example:hits',1
//第二次给计数器的值加1
incr 'demo','1','example:hits',1
//获取计数器的值(注意dummy的使用)
get_counter 'demo','1','example:hits','dummy'
JAVA API示例代码
private Configuration conf
private Connection conn
private Table table
conf = HBaseConfiguration.create()
// zookeeper地址
conf.set("hbase.zookeeper.quorum", "master")
// zookeeper端口
conf.set("hbase.zookeeper.property.clientPort", "2181")
conn = ConnectionFactory.createConnection(conf)
table = conn.getTable(TableName.valueOf("word"))
Get get = new Get(Bytes.toBytes("1"))
get.addColumn(Bytes.toBytes("example"), Bytes.toBytes("hits"))
Result result = table.get(get)
System.out.println(Bytes.toLong(result.getValue(Bytes.toBytes("example"), Bytes.toBytes("hits"))))
注意:输出语句中对getValue的值进行转换时必须使用Bytes.toLong()方法,否则结果报错,一直为0.