您好,登錄后才能下訂單哦!
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表同樣有數據,并且符合設計,則說明二級索引建立成功
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。