分享

Hbase 二级索引插入索引表代码示例

hyj 2014-8-19 02:43:14 发表于 实操演练 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 10432
问题导读:
1.hbase协处理器分为几种?
2.钩子分为几种,各自的作用是什么?
3.你认为如何将数据插入索引表?






HBase在0.92之后引入了coprocessors,提供了一系列的钩子,让我们能够轻易实现访问控制和二级索引的特性。下面简单介绍下两种coprocessors,第一种是Observers,它实际类似于触发器,第二种是Endpoint,它类似与存储过程。由于这里只用到了Observers,所以只介绍Observers,想要更详细的介绍请查阅(https://blogs.apache.org/hbase/entry/coprocessor_introduction)。observers分为三种:

RegionObserver:提供数据操作事件钩子;

WALObserver:提供WAL(write ahead log)相关操作事件钩子;

MasterObserver:提供DDL操作事件钩子。

相关接口请参阅hbase api。

下面给出一个例子,该例子使用RegionObserver实现在写主表之前将索引数据先写到另外一个表:


  1. package com.dengchuanhua.testhbase;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import org.apache.hadoop.hbase.CoprocessorEnvironment;
  6. import org.apache.hadoop.hbase.KeyValue;
  7. import org.apache.hadoop.hbase.client.HTable;
  8. import org.apache.hadoop.hbase.client.Put;
  9. import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
  10. import org.apache.hadoop.hbase.coprocessor.ObserverContext;
  11. import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
  12. import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
  13. public class TestCoprocessor extends BaseRegionObserver {
  14.         private HTable table = null;
  15.        
  16.         @Override
  17.         public void start(CoprocessorEnvironment env) throws IOException {  
  18. //        pool = new HTablePool(env.getConfiguration(), 10);
  19.         table = new HTable(env.getConfiguration(), "test_index");
  20.     }
  21.        
  22.         @Override
  23.         public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e,
  24.                         final Put put, final WALEdit edit, final boolean writeToWAL)
  25.                         throws IOException {
  26.                 // set configuration
  27. //                Configuration conf = new Configuration();
  28.                 // need conf.set...
  29.                 List<KeyValue> kv = put.get("family".getBytes(), "cog".getBytes());
  30.                 Iterator<KeyValue> kvItor = kv.iterator();
  31.                 while (kvItor.hasNext()) {
  32.                         KeyValue tmp = kvItor.next();
  33.                         Put indexPut = new Put(tmp.getValue());
  34.                         indexPut.add("family".getBytes(), "cog".getBytes(), tmp.getRow());
  35.                         table.put(indexPut);
  36.                         table.flushCommits();
  37.                 }
  38.         }
  39.        
  40.         @Override
  41.     public void stop(CoprocessorEnvironment env) throws IOException {
  42.                 table.close();
  43.     }
  44. }
复制代码


写完后要加载到table里面去,先把该文件打包成test.jar并上传到hdfs的/demo路径下,然后操作如下:

1. disable ‘testTable’

2. alter ‘testTable’, METHOD=>’table_att’,'coprocessor’=>’hdfs:///demo/test.jar|com.dengchuanhua.testhbase.TestCoprocessor|1001′

3. enable ‘testTable’

然后往testTable里面插数据就会自动往indexTableName写数据了。


###########################################################
http://491569462-qq-com.iteye.com/blog/1923166

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条