码迷,mamicode.com
首页 > 其他好文 > 详细

HBASE二级索引

时间:2016-05-10 18:54:32      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:hbse 二级索引

HBASE基于coprocessor实现二级索引


场景如下:存储UC_TWEETS表,ROWKEY设计:folderId_dayId_siteId_docId,导出有如下需求:根据campaignId导出,所以需要存储campaignId的索引表

实现步骤如下:

一, 代码实现如下:

public class HbaseCoprocessor extends BaseRegionObserver {

@Override

public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e, final Put put, 

final WALEdit edit, final Durability durability) throws IOException {

Configuration configuration = HBaseConfiguration.create();

configuration.set("hbase.regionserver.lease.period", "900000");  

        configuration.set("hbase.rpc.timeout", "1800000");  

        configuration.set("hbase.client.scanner.timeout.period", "1800000");

        configuration.set("hbase.zookeeper.property.clientPort", "2181");  

        configuration.set("hbase.zookeeper.quorum", "DEV-HADOOP-01,DEV-HADOOP-02,DEV-HADOOP-03");  

        configuration.set("hbase.master", "DEV-HADOOP-01:60000");

        

        HTable table = new HTable(configuration, "UC_INDEX");

   List<Cell> kv = put.get("f1".getBytes(), "campaignId".getBytes()); 

   Iterator<Cell> kvItor = kv.iterator(); 

   while (kvItor.hasNext()) { 

    KeyValue tmp = (KeyValue)kvItor.next(); 

    String rowkey = new String(tmp.getRow());

    String value = new String(tmp.getValue());

    String newRowkey = value + "_" + rowkey;

   Put indexPut = new Put(newRowkey.getBytes()); 

   indexPut.add("f1".getBytes(), tmp.getQualifier(), tmp.getValue()); 

   table.put(indexPut); 

   } 

   table.close();

}


}


二, 把上面的HbaseCoprocessor类导出.选择Export -> Jar File,导出成ucTweet.jar文件

三, 把ucTweet.jar文件上传到HDFS,命令如下:./hadoop fs -put /data/server/ucTweet_index.jar /jars

四, 设置UC_TWEETS表的coprocessor属性,命令如下:alter ‘UC_TWEETS‘,METHOD=>‘table_att‘,‘coprocessor‘=>‘hdfs://192.168.9.110:9000/jars/ucTweet.jar|com.prime.dsc.inputservice.coprocessor.HbaseCoprocessor|1001|‘

五, 把数据插入UC_TWEETS表,如果UC_INDEX表同样有数据,并且符合设计,则说明二级索引建立成功

HBASE二级索引

标签:hbse 二级索引

原文地址:http://beyond3518.blog.51cto.com/1360525/1771889

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!