`

hbase coprocessor入门使用 转

阅读更多

转:http://www.360doc.com/content/14/0402/17/16635465_365774770.shtml

1.起因(Why HBase  Coprocessor)

HBase作为列族数据库最经常被人诟病的特性包括:无法轻易建立“二级索引”,难以执行求和、计数、排序等操作。比如,在旧版本的(<0.92)Hbase中,统计数据表的总行数,需要使用Counter方法,执行一次MapReduce Job才能得到。虽然HBase在数据存储层中集成了MapReduce,能够有效用于数据表的分布式计算。然而在很多情况下,做一些简单的相加或者聚合计算的时候,如果直接将计算过程放置在server端,能够减少通讯开销,从而获得很好的性能提升。于是,HBase在0.92之后引入了协处理器(coprocessors),实现一些激动人心的新特性:能够轻易建立二次索引、复杂过滤器(谓词下推)以及访问控制等。

2.灵感来源( Source of Inspration)

HBase协处理器的灵感来自于Jeff Dean 09年的演讲( P66-67)。它根据该演讲实现了类似于bigtable的协处理器,包括以下特性:

  • 每个表服务器的任意子表都可以运行代码
  • 客户端的高层调用接口(客户端能够直接访问数据表的行地址,多行读写会自动分片成多个并行的RPC调用)
  • 提供一个非常灵活的、可用于建立分布式服务的数据模型
  • 能够自动化扩展、负载均衡、应用请求路由
HBase的协处理器灵感来自bigtable,但是实现细节不尽相同。HBase建立了一个框架,它为用户提供类库和运行时环境,使得他们的代码能够在HBase region server和master上处理。

3.细节剖析(Implementation)

协处理器分两种类型,系统协处理器可以全局导入region server上的所有数据表,表协处理器即是用户可以指定一张表使用协处理器。协处理器框架为了更好支持其行为的灵活性,提供了两个不同方面的插件。一个是观察者(observer),类似于关系数据库的触发器。另一个是终端(endpoint),动态的终端有点像存储过程。

 3.1观察者(Observer)

观察者的设计意图是允许用户通过插入代码来重载协处理器框架的upcall方法,而具体的事件触发的callback方法由HBase的核心代码来执行。协处理器框架处理所有的callback调用细节,协处理器自身只需要插入添加或者改变的功能。

以HBase0.92版本为例,它提供了三种观察者接口:

  • RegionObserver:提供客户端的数据操纵事件钩子:Get、Put、Delete、Scan等。
  • WALObserver:提供WAL相关操作钩子。
  • MasterObserver:提供DDL-类型的操作钩子。如创建、删除、修改数据表等。

这些接口可以同时使用在同一个地方,按照不同优先级顺序执行.用户可以任意基于协处理器实现复杂的HBase功能层。HBase有很多种事件可以触发观察者方法,这些事件与方法从HBase0.92版本起,都会集成在HBase API中。不过这些API可能会由于各种原因有所改动,不同版本的接口改动比较大,具体参考Java Doc

RegionObserver工作原理,如图1所示。更多关于Observer细节请参见HBaseBook的第9.6.3章节

regionobserver.png

图1 RegionObserver工作原理

 

3.2终端(Endpoint)

终端是动态RPC插件的接口,它的实现代码被安装在服务器端,从而能够通过HBase RPC唤醒。客户端类库提供了非常方便的方法来调用这些动态接口,它们可以在任意时候调用一个终端,它们的实现代码会被目标region远程执行,结果会返回到终端。用户可以结合使用这些强大的插件接口,为HBase添加全新的特性。终端的使用,如下面流程所示:

  1. 定义一个新的protocol接口,必须继承CoprocessorProtocol.
  2. 实现终端接口,该实现会被导入region环境执行。
  3. 继承抽象类BaseEndpointCoprocessor.
  4. 在客户端,终端可以被两个新的HBase Client API调用 。单个region:HTableInterface.coprocessorProxy(Class<T> protocol, byte[] row) 。rigons区域:HTableInterface.coprocessorExec(Class<T> protocol, byte[] startKey, byte[] endKey, Batch.Call<T,R> callable)

整体的终端调用过程范例,如图2所示:

rpc.png

图2 终端调用过程范例

4.编程实践(Code Example)

在该实例中,我们通过计算HBase表中行数的一个实例,来真实感受协处理器 的方便和强大。在旧版的HBase我们需要编写MapReduce代码来汇总数据表中的行数,在0.92以上的版本HBase中,只需要编写客户端的代码即可实现,非常适合用在WebService的封装上。

4.1启用协处理器 Aggregation(Enable Coprocessor Aggregation)

我们有两个方法: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 来实现。

(1)disable指定表。hbase> disable 'mytable'

(2)添加aggregation hbase> alter 'mytable', METHOD => 'table_att','coprocessor'=>'|org.apache.hadoop.hbase.coprocessor.AggregateImplementation||'

(3)重启指定表 hbase> enable 'mytable'

4.2统计行数代码(Code Snippet)

复制代码
public class MyAggregationClient { 

private static final byte[] TABLE_NAME = Bytes.toBytes("mytable");
private static final byte[] CF = Bytes.toBytes("vent");
public static void main(String[] args) throws Throwable {
Configuration customConf = new Configuration();
customConf.setStrings("hbase.zookeeper.quorum",
"node0,node1,node2");
//提高RPC通信时长
customConf.setLong("hbase.rpc.timeout", 600000);
//设置Scan缓存
customConf.setLong("hbase.client.scanner.caching", 1000);
Configuration configuration = HBaseConfiguration.create(customConf);
AggregationClient aggregationClient = new AggregationClient(
configuration);
Scan scan = new Scan();
//指定扫描列族,唯一值
scan.addFamily(CF);
long rowCount = aggregationClient.rowCount(TABLE_NAME, null, scan);
System.out.println("row count is " + rowCount);

}
}
复制代码

 

 
 
以下是关于Observer程序示例的补充:

4.编程实践(Code Example)

4.3 RegionObserverExample (此例来自《HBase: The Definitive Guide》)
//新实现的类须继承BaseRegionObserver类
package hbaseCoprocessor;
 
import java.io.IOException;  
import java.util.List;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;  
import org.apache.hadoop.hbase.client.Get;  
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
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.util.Bytes;  
  
public class RegionObserverExample extends  
    BaseRegionObserver {  
      
    public static final byte[] FIXED_ROW =  
            Bytes.toBytes("@@@GETTIME@@@");
    public static String tablename = "table";
    public static String rowkey = "rowkey";
    @Override  
    public void preGet(  
            final ObserverContext<RegionCoprocessorEnvironment> e,  
            final Get get, final List<KeyValue> results) throws  
            IOException {  
                //if (Bytes.equals(get.getRow(), FIXED_ROW)) {   //书中原来的功能是如果查询的row为FIXED_ROW时,在结果返回系统时间
                    KeyValue kv = new KeyValue(get.getRow(), FIXED_ROW,  
                            FIXED_ROW,  
                            Bytes.toBytes(System.currentTimeMillis()));  
                    results.add(kv);  
                //}  
    }  
  public static void selectRow(String tablename, String rowKey)
    throws IOException {
      Configuration config = HBaseConfiguration.create();
      HTable table =new HTable(config, tablename);
      Get g =new Get(rowKey.getBytes());
      Result rs = table.get(g);
      for (KeyValue kv : rs.raw()) {
        System.out.print(new String(kv.getRow()) +" ");
        System.out.print(new String(kv.getFamily()) +":");
        System.out.print(new String(kv.getQualifier()) +" ");
        System.out.println(new String(kv.getValue()));
      }
      table.close();
  }
  public static void main(String args[]){  
    try {
      selectRow( tablename, rowkey);
    } catch (IOException e) {
      e.printStackTrace();
    }
    System.out.println("sucess!");  
  }    
}  
1.编译通过后,将该类打包成jar文件(如test.jar),并copy到各regionserver的安装目录下,
2 利用shell命令加载此coprocessor到特定表上: alter 't1', METHOD => 'table_att', 'coprocessor'=>'test.jar|hbaseCoprocessor.RegionObserverExample|1001|'.
3 然后在客户端执行上面的程序,即可得到预期结果。
4 一个疑问,根据参考资料中说明,可以将jar文件上传到hdfs中(命令如下),再加载到表上,但我并没有成功。
 alter 't1', METHOD => 'table_att', 'coprocessor'=>'hdfs:///test.jar|hbaseCoprocessor.RegionObserverExample|1001|'.
ps:经过尝试,可以将路径写完整,即:
alter 't1', METHOD => 'table_att', 'coprocessor'=>'hdfs://nnip:9000/test.jar|hbaseCoprocessor.RegionObserverExample|1001|'.
根据自己NN的配置,将上面的nnip修改即可正确运行cp.
5 删除一个coprocessor的shell命令:alter 't1', METHOD => 'table_att_unset',NAME => 'coprocessor$1'
 
参考资料:
HBase: The Definitive Guide
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics