HBase_HBase2.0 Java API 操作指南 (五) 计数器

HBase 的计数器在 点击流和广告统计中非常常用。本篇文章我们将从 shell 和 java API 两个方面去探索 Hbase 的计数器的使用。

 

1.shell 操作

2.JavaApi

   i.单计数器

  ii.多计数器

 

0.计数器介绍

在Hase 中,计数器机制是一种原子操作,需要注意的是,计数器是面向列的操作。即每次对特定计数器的操作只会锁住一列,而不是一行。然后读取数据,在对当前数据进行加法操作,最后再写入Hbase并释放该列的锁。在操作的过程中用户是可以访问这一行的其他数据的,否则如果用户对一整行的数据加锁然后读取数据,会造成大量资源抢占问题,这在一个高负载的系统中是致命的。

 

 

1.shell 操作

 

 创建一张测试表 表名 hits, 拥有 pu, uv 两个列族

create 'hits','pv','uv'

 

创建并修改计数器

NOTE : 没有计数器初始化单独的指令,初始化和操作指令相同

incr 'hits','20200424','pv:1',1
incr 'hits','20200424','uv:1',2

 

获取计数器的值

 get_counter 'hits','20200424','uv:1'

输出:

hbase(main):002:0> get_counter 'hits','20200424','uv:1'
COUNTER VALUE = 2
Took 0.8213 seconds   

 

扫描表

scan 'hits'

ROW                         COLUMN+CELL                                                                  
 20200423                   column=uv:1, timestamp=1587662324121, value=\x00\x00\x00\x00\x00\x00\x00\x04 
 20200424                   column=pv:1, timestamp=1587661726573, value=\x00\x00\x00\x00\x00\x00\x00\x01 
 20200424                   column=uv:1, timestamp=1587661734932, value=\x00\x00\x00\x00\x00\x00\x00\x02 
2 row(s)
Took 0.0296 seconds  

注意:在表中存储的数据实际是 bytes 字节数组,所以会看到数据实际上是不可直接读的。

 

 

操作计数器的指令

incr 'table' 'rowKey' 'columnFamily:column' 'increment-value'

'increment-value' 不同的值对计数器产生的影响

比零大的值                 按给定值增加计数器中的数值
零                               得到计数器当前值,与Shell命令get_counter的返回值相同
比零大的值                减少计数器的当前值

 

=========================================

 

2.JavaAPI

   i.单计数器

单计数器的相关Java API

  /**
   * See {@link #incrementColumnValue(byte[], byte[], byte[], long, Durability)}
   * <p>
   * The {@link Durability} is defaulted to {@link Durability#SYNC_WAL}.
   * @param row The row that contains the cell to increment.
   * @param family The column family of the cell to increment.
   * @param qualifier The column qualifier of the cell to increment.
   * @param amount The amount to increment the cell with (or decrement, if the
   * amount is negative).
   * @return The new value, post increment.
   * @throws IOException if a remote or network exception occurs.
   */
  long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier,
    long amount) throws IOException;

  /**
   * Atomically increments a column value. If the column value already exists
   * and is not a big-endian long, this could throw an exception. If the column
   * value does not yet exist it is initialized to <code>amount</code> and
   * written to the specified column.
   *
   * <p>Setting durability to {@link Durability#SKIP_WAL} means that in a fail
   * scenario you will lose any increments that have not been flushed.
   * @param row The row that contains the cell to increment.
   * @param family The column family of the cell to increment.
   * @param qualifier The column qualifier of the cell to increment.
   * @param amount The amount to increment the cell with (or decrement, if the
   * amount is negative).
   * @param durability The persistence guarantee for this increment.
   * @return The new value, post increment.
   * @throws IOException if a remote or network exception occurs.
   */
  long incrementColumnValue(byte[] row, byte[] family, byte[] qualifier,
    long amount, Durability durability) throws IOException;

注意 Java API 中也不存在对计数器初始化的api

如果想初始化一个计数器,可以像下面这样操作

long value2 = table.incrementColumnValue(Bytes.toBytes("20200423"),Bytes.toBytes("uv"),Bytes.toBytes("2"),0);
System.out.println(value2);

其中函数的返回值会返回计数器在修改过后的值

 

 

 

  ii.多计数器

另一个增加计数器的途径,是 table 的 increment() 方法。该方法可以操作多列数据。

首先我们需要创建一个Increment 对象,并把需要操作的装载进去。

Increment multiIncrement = new Increment(Bytes.toBytes("20200224"));
multiIncrement.addColumn(Bytes.toBytes("pv"),Bytes.toBytes("1"),-1);
multiIncrement.addColumn(Bytes.toBytes("uv"),Bytes.toBytes("1"),1);
multiIncrement.addColumn(Bytes.toBytes("pv"),Bytes.toBytes("2"),1);
multiIncrement.addColumn(Bytes.toBytes("uv"),Bytes.toBytes("2"),4);
Result result = table.increment(multiIncrement);

 

 

单计数器 与 多计数器 API操作示例

package hbase_2.counter;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

/**
 * Created by szh on 2020/4/24.
 * @author szh
 */
public class Hbase_Counter {

    public static void main(String[] args) throws Exception {

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "cdh-manager,cdh-node1,cdh-node2");
        conf.set("hbase.zookeeper.property.clientPort", "2181");

        Connection conn = ConnectionFactory.createConnection(conf);
        TableName tableName = TableName.valueOf("hits");

        Table table = conn.getTable(tableName);
        //设置客户端缓存大小

        long value = table.incrementColumnValue(Bytes.toBytes("20200423"),Bytes.toBytes("uv"),Bytes.toBytes("1"),4);
        System.out.println(value);

        long value2 = table.incrementColumnValue(Bytes.toBytes("20200423"),Bytes.toBytes("uv"),Bytes.toBytes("2"),0);
        System.out.println(value2);


        Increment multiIncrement = new Increment(Bytes.toBytes("20200224"));
        multiIncrement.addColumn(Bytes.toBytes("pv"),Bytes.toBytes("1"),-1);
        multiIncrement.addColumn(Bytes.toBytes("uv"),Bytes.toBytes("1"),1);
        multiIncrement.addColumn(Bytes.toBytes("pv"),Bytes.toBytes("2"),1);
        multiIncrement.addColumn(Bytes.toBytes("uv"),Bytes.toBytes("2"),4);
        Result result = table.increment(multiIncrement);

        for(Cell cell : result.rawCells()){
            System.out.println(cell);
        }

        table.close();

    }
}

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值