實現HBase二級索引的配置與使用--ITHBase

遇到自己眼中亂如麻、大如山的問題,保持平常心,不要急躁,冷靜的分析思考尋找突破點,解決之後,發現原來那些如山如麻般的問題其實只是小土丘子、小線團子

一 ITHbase的安裝配置

ITHbase的全稱是 Indexed Transactional HBase,事務性是它的重要特性。

1.編譯

(1)用eclipse作爲項目打開hbase-trx-hbase-transactional-tableindexed-915fd07.tar.gz解壓後的源代碼文件夾src

(2)添加jar包。

    添加hbase,hadoop,zookeeper,logging相關的四個jar包。現在用的是開源版本的 hbase-0.90.4

 

(3)添加後仍有很多錯誤和警告,錯誤都是和一個問題有關:將所有Progressable類型變量 轉爲 CancelableProgressable,爲此作出瞭如下修改:

修改的方法:

package org.apache.hadoop.hbase.regionserver.transactional;

class TransactionalRegion

protected HRegion openHRegion(final CancelableProgressable reporter) throws IOException {

protected long replayRecoveredEditsIfAny(final Path regiondir, final long minSeqId, final CancelableProgressable reporter)  

 protected void doReconstructionLog(final Path oldCoreLogFile, final long minSeqId, final long maxSeqId,

            final CancelableProgressable reporter)

添加的方法:

package org.apache.hadoop.hbase.regionserver.transactional; 

class THLogRecoveryManager

//重載了一個方法,參數Progressable 轉爲 CancelableProgressable,其他的都沒變

public Map<Long, WALEdit> getCommitsFromLog(final Path reconstructionLog, final long minSeqID,

            final CancelableProgressable reporter) throws UnsupportedEncodingException, IOException

警告是因爲一些方法被標爲deprecated,以後不用了,現在可以用,但最好用新方法,暫時先沒改。

(4)打成jar包 ithbase-0.90.4-hik.jar

 

2.安裝配置

    將jar包copy到集羣中所有服務器的 $HBASE_HOME/lib 下;

配置$HBASE_HOME/conf/hbase-site.xml文件,添加如下內容:

<property>
<name>hbase.hlog.splitter.impl</name>
<value>org.apache.hadoop.hbase.regionserver.transactional.THLogSplitter</value>
</property>
<property>
<name>hbase.regionserver.class</name>
<value>org.apache.hadoop.hbase.ipc.IndexedRegionInterface</value>
</property>
<property>
<name>hbase.regionserver.impl</name>
<value>org.apache.hadoop.hbase.regionserver.tableindexed.IndexedRegionServer</value>
</property>
<property>
<name>hbase.hregion.impl</name>
<value>org.apache.hadoop.hbase.regionserver.tableindexed.IndexedRegion</value>
</property>

 

3.重啓hbase

 

如此準備工作便完成了。

需要注意的是,如果用在 hbase-0.90.3 版本上,需要選擇jar包重新編譯。


 

二 ITHbase的使用

1.索引特性:

(1) 對主表進行插入刪除操作,索引表會自動進行相應操作;

(2) 對主表索引列進行修改操作,索引表會自動進行相應調整;若在主表中刪除索引列,則索引表中索引不到。(插入數據的規範性)

(3) 可以直接對索引表插入刪除,主表不會變(看是否有相關設置以保證不能對索引表進行單獨操作)。

2.單列索引

       IndexedTableAdmin indexedAdmin = new IndexedTableAdmin(config);

       HTableDescriptor desc = new HTableDescriptor("car");

       desc.addFamily(new HColumnDescriptor("carInfo"));     

 

       IndexedTableDescriptor indexDesc = new IndexedTableDescriptor(desc);

        //Specification

       IndexSpecification indexSpec = new IndexSpecification("index", Bytes.toBytes("carInfo:data"));       

       

        indexDesc.addIndex(indexSpec);

 

       

        indexedAdmin.setBatchSize(10); // To fully test re-indexing

        indexedAdmin.createIndexedTable(indexDesc);

3.多列索引

自己寫一個類,實現 IndexKeyGenerator 接口,實現兩列值的組合索引功能。即支持兩列值作爲索引key。需要將其打成jar包並copy到集羣中所有服務器的 $HBASE_HOME/lib 下。

使用方法如下:

IndexedTableAdmin indexedAdmin = new IndexedTableAdmin(config);

HTableDescriptor desc = new HTableDescriptor("car");

desc.addFamily(new HColumnDescriptor("carInfo"));       

 

IndexedTableDescriptor indexDesc = new IndexedTableDescriptor(desc);

        // Create a new index that does lexicographic ordering on COL_A

        //Specification

byte[] column1 = Bytes.toBytes("carInfo:data");

byte[] column2 = Bytes.toBytes("carInfo:type");             

IndexSpecification indexSpec = new IndexSpecification("busindex",new byte[][]{column1, column2},

                            null, new MultiIndexKeyGenerator(column1, column2));

indexDesc.addIndex(indexSpec);

indexedAdmin.setBatchSize(10); // To fully test re-indexing

indexedAdmin.createIndexedTable(indexDesc);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章