概念
hbase包含两种协处理器:Observers和Endpoint。
其中Observers可以理解问传统数据库的触发器,当发生某一个特定操作的时候出发Observer。
具体方法不作过多介绍,根据个人需求,以下是部分方法执行的时序图
其中Endpoint可以理解为传统数据库的存储过程操作,比如可以进行某族某列值得加和。无Endpoint特性的情况下需要全局扫描表,通过Endpoint则可以在多台分布有对应表的regionserver上同步加和,在江加和数返回给客户端进行全局加和操作,充分利用了集群资源,增加性能。Endpoint基本概念如下图:
2. 两者代码实现细节的差异
在实现两种协处理器的时候稍有区别。无论哪种协处理器都需要运行于Server端的环境中。其中Endpoint还需要通过protocl来定义接口实现客户端代码进行rpc通信以此来进行数据的搜集归并。而Observer则不需要客户端代码,只在特定操作发生的时候出发服务端代码的实现。
3. Observer协处理器的实现
hbase版本1.1.2
以下例子为table级的协处理器,还有一种针对整个hbase的,篇幅所限,如有需要,在今后的章节会进行介绍
1.引入依赖
<properties>
<hbase.version>1.1.2</hbase.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>${hbase.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>${hbase.version}</version>
</dependency>
</dependencies>
2.打包配置
<build>
<plugins>
<!--assembly集成依赖打包-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!--设定主类-->
<archive>
<manifest>
<mainClass>Coprocessor.CoprocessorDemo.java</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.核心代码
Observer主要是继承BaseRegionObserver类,具体重写哪个方法看个人需求
public class CoprocessorDemo extends BaseRegionObserver {
private static final Log LOG = LogFactory.getLog(CoprocessorDemo.class);
@Override
public void start(CoprocessorEnvironment e) throws IOException {
Configuration conf = e.getConfiguration();
String cluster = conf.get("es_cluster");
LOG.warn("------observer init EsClient ------"+cluster);
}
@Override
public void stop(CoprocessorEnvironment e) throws IOException {
LOG.warn("------observer init EsClient ------");
}
@Override
public void postPut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit, Durability durability) throws IOException {
String indexId = new String(put.getRow());
LOG.warn("put a new data to hbase "+indexId);
}
@Override
public boolean postBulkLoadHFile(ObserverContext<RegionCoprocessorEnvironment> ctx, List<Pair<byte[], String>> familyPaths, boolean hasLoaded) throws IOException {
for (Pair<byte[], String> p:familyPaths) {
String key = new String(p.getFirst());
String value = new String(p.getSecond());
LOG.warn(key+value);
}
return hasLoaded;
}
}
以上代码分别重写了start、stop、postPut、postBulkLoadHFile方法,具体作用可查看官网,这里不多说,代码开发好了之后打成jar包上传到hbase集群,执行如下命令,注意改一下主类和jar包名字
create 'test_record','info'
disable 'test_record'
alter 'test_record' , METHOD =>'table_att','coprocessor'=>'hdfs:///hbase_es/Observer_bulkload.jar|Coprocessor.CoprocessorDemo|1073741823|es_cluster=zoe-es'
enable 'test_record'
put 'test_record','test1','info:c1','value1'
想看是否成功,可到hbase ui界面查看日志,这里就不贴图了,接下来讲一下卸载协处理器
disable 'test_record'
alter 'test_record', METHOD => 'table_att_unset',NAME => 'coprocessor$1'
enable 'test_record'
desc 'test_record'
以上就是协处理器table级的用法介绍