实现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);

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