HBASE二级索引-创新互联

HBASE基于coprocessor实现二级索引

成都创新互联专业为企业提供正定网站建设、正定做网站、正定网站设计、正定网站制作等企业网站建设、网页设计与制作、正定企业网站模板建站服务,10多年正定做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

场景如下:存储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表同样有数据,并且符合设计,则说明二级索引建立成功

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前文章:HBASE二级索引-创新互联
URL网址:http://pwwzsj.com/article/dedsgp.html