HBase Coprocessor 之 RegionObserver(hbase 0.96.0)
2014-04-29 10:15
465 查看
关于hbase的协处理器(Coprocessor )的介绍在此不做赘述,本文基于RegionObserver实现hbase的计数器,用于实时统计pv等关键指标。
一、背景介绍:
RegionObserver:hbase协处理器的一种,类似于关系型数据库的存储过程;
Counter:hbase的计数器,线程安全,本质上就是一张表
二、实现原理
使用hbase的触发器,当执行put操作后,计数器加一;执行delete操作后,计数器减一;
三、代码
如果一次更新多条记录,参考以下代码:
四、部署
详见:HBase Coprocessor 之 endpiont(hbase 0.96.0)
一、背景介绍:
RegionObserver:hbase协处理器的一种,类似于关系型数据库的存储过程;
Counter:hbase的计数器,线程安全,本质上就是一张表
二、实现原理
使用hbase的触发器,当执行put操作后,计数器加一;执行delete操作后,计数器减一;
三、代码
package com.cxk.coprocessor.test; import java.io.IOException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Delete; import org.apache.hadoop.hbase.client.Durability; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver; import org.apache.hadoop.hbase.coprocessor.ObserverContext; import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; import org.apache.hadoop.hbase.regionserver.wal.WALEdit; import org.apache.hadoop.hbase.util.Bytes; public class TestRegionObserver extends BaseRegionObserver{ @Override public void postDelete(ObserverContext<RegionCoprocessorEnvironment> e, Delete delete, WALEdit edit, Durability durability) throws IOException { // TODO Auto-generated method stub super.postDelete(e, delete, edit, durability); HTable table=new HTable(e.getEnvironment().getConfiguration(),"sumcounter"); table.incrementColumnValue(Bytes.toBytes("row1"), Bytes.toBytes("cf"), Bytes.toBytes("count"),(long)-1); table.close(); } @Override public void postPut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit, Durability durability) throws IOException { // TODO Auto-generated method stub super.postPut(e, put, edit, durability); HTable table=new HTable(e.getEnvironment().getConfiguration(),"sumcounter"); table.incrementColumnValue(Bytes.toBytes("row1"), Bytes.toBytes("cf"), Bytes.toBytes("count"),(long)1); table.close(); } }
如果一次更新多条记录,参考以下代码:
Increment increment=new Increment(Bytes.toBytes(row)); increment.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier), (long)2); increment.addColumn(Bytes.toBytes(family), Bytes.toBytes(qualifier2), (long)3); Result result=table.increment(increment);
四、部署
详见:HBase Coprocessor 之 endpiont(hbase 0.96.0)
相关文章推荐
- HBase报错:No jar path specified for org.apache.phoenix.coprocessor.UngroupedAggregateRegionObserver
- HBase Coprocessor 之 endpiont(hbase 0.96.0)
- HBase Coprocessor 之 endpiont(hbase 0.96.0)
- Hbase0.98.4中部署用户自定义的Observer Coprocessor
- Hbase0.98.4中部署用户自定义的Observer Coprocessor
- trafodion Exception ERROR Class org.apache.hadoop.hbase.coprocessor.transactional.TrxRegionEndpoint
- hbase coprocessor 实践:observer
- HBase Coprocessor 之 endpiont(hbase 0.96.0)
- HBase 协处理器---基本概念和regionObserver的简单实现
- 玩转HBase: Coprocessor Endpoint (1):startkey和endkey的真正作用
- 理解HBASE COPROCESSOR笔记
- Hbase Endpoint Coprocessor
- namenode做了ha后kylin出现错误No registered coprocessor service found for name CubeVisitService in region
- hbase coprocessor小实践引发的对coprocessor异常处理机制的探究
- HBase CoProcessor介绍以及使用
- Hbase coprocessor获取数据
- HBase Coprocessor 协处理器
- hbase coprocessor的分析
- HBase Coprocessor Endpint运行机制
- hbase 自定义 endpoint coprocessor