【HBase】1009-HBase的协处理器(coprocessor)统计行数

引入Hbase中的Coprocessor 的原因

HBase作为列族数据库无法建立“二级索引”,难以执行求和、计数、排序等操作。为解决这些问题,HBase0.92 之后引入协处理器(Coprocessor),实现一些新特性,能够轻易建立二次索引、复杂过滤器、以及访问控制。



协处理器两个插件

 

1)观察者(observer)

 

提供三种观察者接口:

RegionObserver:提供客户端的数据操纵事件钩子:Get、Put、Delete、Scan等。

WALObserver:提供WAL相关操作钩子。

MasterObserver:提供DDL-类型的操作钩子。如创建、删除、修改数据表等。

 

2)终端(endpoint)

EndPoint协处理器

(1)ObServer协处理器:允许集群在正常的客户端操作过程中可以有不同的行表现!

(2)EndPoint协处理器:允许你扩展集群能力,对客户端应用开放新的运行命令,在RegionServer上执行

HBase的协处理器(coprocessor)统计函数
(1)在使用HBase的协处理器(coprocessor)之前,需要启动协处理器,有两种方案。
方案1:启动全局aggregation,能过操纵所有的表上的数据。通过修改hbase-site.xml这个文件来实现
<property>
     <name>hbase.coprocessor.user.region.classes</name>
     <value>org.apache.hadoop.hbase.coprocessor.AggregateImplementation</value>
</property>

方案2:启用表aggregation,只对特定的表生效。通过HBase Shell 来实现
create 'stu', {NAME => 'info', VERSIONS => 5}
1、disable指定表。
hbase> disable 'stu'
2、添加aggregation
hbase>alter 'stu', METHOD => 'table_att','coprocessor'=>'|org.apache.hadoop.hbase.coprocessor.AggregateImplementation||'
3、重启指定表
hbase> enable 'stu'

(2)JAVA代码统计表中列族的行数

public class MyAggregationClient {

     public static void main(String[] args) throws Throwable {
          Configuration customConf = new Configuration();
          customConf.set("hbase.rootdir", "hdfs://mycluster:8020/hbase");
          customConf.setStrings("hbase.zookeeper.quorum", "mycluster:2181");
          // 提高RPC通信时长
          customConf.setLong("hbase.rpc.timeout", 600000);
          // 设置Scan缓存
          customConf.setLong("hbase.client.scanner.caching", 1000);
          // 默认为9000毫秒
          customConf.set("zookeeper.session.timeout", "180000");
          Configuration configuration = HBaseConfiguration.create(customConf);
          AggregationClient aggregationClient = new AggregationClient(configuration);
          Scan scan = new Scan();
          // 指定扫描列族,唯一值
          scan.addFamily(Bytes.toBytes("info"));
          long rowCount = aggregationClient.rowCount(TableName.valueOf("stu"),new LongColumnInterpreter(), scan);
          System.out.println("row count is " + rowCount);
     }
}


HBase的协处理器案例
协处理器其中的一个作用是使用Observer创建二级索引

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

艾文教编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值