HBASE二级索引

HBASE基于coprocessor实现二级索引

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了可克达拉免费建站欢迎大家使用!

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

实现步骤如下:

一, 代码实现如下:

public class HbaseCoprocessor extends BaseRegionObserver {

@Override

public void prePut(final ObserverContext 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 kv = put.get("f1".getBytes(), "campaignId".getBytes()); 

   Iterator 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二级索引
链接URL:http://cdiso.cn/article/gphedg.html

其他资讯