hbase-Coprocessor(协处理器)

概念

hbase包含两种协处理器:ObserversEndpoint。
其中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级的用法介绍

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

局外人一枚

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

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

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

打赏作者

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

抵扣说明:

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

余额充值