HBase二级索引实现方案

Hbase简介

HBASE是在hadoop之上构建非关系型,面向列存储的开源分布式结构化数据存储系统。

HBase表分区与索引管理

 

•将Table中的数据根据rowKey字段划分为多个HRegion

•HRegion分配给RegionServer管理

HBase系统架构

 

HBase的局限性

HBase本身只提供基于行键和全表扫描的查询,而行键索引单一,对于多维度的查询困难。

常见的二级索引方案

HBase的一级索引就是rowkey,我们只能通过rowkey进行检索。如果我们相对hbase里面列族的列列进行一些组合查询,就需要采用HBase的二级索引方案来进行多条件的查询。 

1. MapReduce方案 
2. ITHBASE(Indexed-Transanctional HBase)方案 
3. IHBASE(Index HBase)方案 
4. Hbase Coprocessor(协处理器)方案 
5. Solr+hbase方案

6. CCIndex(complementalclustering index)方案

HBase二级索引种类

2.1创建单列索引

2.2同时创建多个单列索引

2.3创建联合索引(最多同时支持3个列)

2.4只根据rowkey创建索引

建立全局二级索引

1. 全局建立索引,可以修改hbase-site.xml文件

为所有table加载了一个cp class,可以用”,”分割加载多个class

<property>

<name>hbase.coprocessor.region.classes</name>

<value>org.apache.hadoop.hbase.coprocessor.AggregateImplementation</value>

</property>

单表建立二级索引

2. 单个表建立索引

1.首先disable ‘表名’
2.然后修改表

alter 'LogTable',METHOD=>'table_att','coprocessor'=>'hdfs:///test.jar|www.aboutyun.com.hbase.HbaseCoprocessor|1001'

3. enable '表名'

卸载二级索引

3. 卸载索引

alter 'LogTable', METHOD => 'table_att_unset', NAME => 'coprocessor$1‘

二级索引的设计

设计思路:

 

图1

二级索引的本质就是建立各列值与行键之间的映射关系

 

如上图1,当要对F:C1这列建立索引时,只需要建立F:C1各列值到其对应行键的映射关系,如C11->RK1等,这样就完成了对F:C1列值的二级索引的构建,当要查询符合F:C1=C11对应的F:C2的列值时(即根据C1=C11来查询C2的值,图1青色部分)

其查询步骤如下:

1. 根据C1=C11到索引数据中查找其对应的RK,查询得到其对应的RK=RK1

2. 得到RK1后就自然能根据RK1来查询C2的值了 这是构建二级索引大概思路,其他组合查询的联合索引的建立也类似。

 

MapReduce方式创建二级索引

使用整合MapReduce的方式创建hbase索引。主要的流程如下:

1.1扫描输入表,使用hbase继承类TableMapper

1.2获取rowkey和指定字段名称和字段值

1.3创建Put实例, value=rowkey, rowkey=columnName +"_" +columnValue

1.4使用IdentityTableReducer将数据写入索引表

继承TableMapper

GenerateIndexMapper继承TableMapper类

LoadIndexMapper类数据批量导入hbase

SecondIndexMain是驱动类

实例


 
 
  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.hbase.HBaseConfiguration;
  3. import org.apache.hadoop.hbase.client.Put;
  4. import org.apache.hadoop.hbase.client.Result;
  5. import org.apache.hadoop.hbase.client.Scan;
  6. import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
  7. import org.apache.hadoop.hbase.mapreduce.MultiTableOutputFormat;
  8. import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
  9. import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
  10. import org.apache.hadoop.hbase.mapreduce.TableMapper;
  11. import org.apache.hadoop.hbase.util.Bytes;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.util.GenericOptionsParser;
  14. import java.io.IOException;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.Set;
  18. /**
  19. * @Description:Mapreduce构建hbase二级索引
  20. */
  21. public class MyIndexBuilder {
  22. private class MyIndexMapper extends TableMapper<ImmutableBytesWritable, Put> {
  23. //create the map object
  24. private Map< byte[], ImmutableBytesWritable> indexes = new HashMap< byte[], ImmutableBytesWritable>();
  25. //make the cloumnfamily
  26. private String columnFamily;
  27. /**
  28. * Called once for each key/value pair in the input split. Most applications
  29. * should override this, but the default is the identity function.
  30. */
  31. @Override
  32. protected void map(ImmutableBytesWritable key, Result value, Context context) throws IOException, InterruptedException {
  33. Set< byte[]> keys = indexes.keySet();
  34. for ( byte[] k : keys) {
  35. ImmutableBytesWritable indexTableName = indexes.get(k);
  36. byte[] val = value.getValue(Bytes.toBytes(columnFamily), k);
  37. // 索引表的rowkey为原始表的值
  38. Put put = new Put(val);
  39. // 索引表的内容为原始表的rowkey
  40. put.add(Bytes.toBytes( "f1"), Bytes.toBytes( "id"), key.get());
  41. //context write
  42. context.write(indexTableName, put);
  43. }
  44. // super.map(key, value, context);
  45. }
  46. /**
  47. * Called once at the beginning of the task.
  48. */
  49. @Override
  50. protected void setup(Context context) throws IOException, InterruptedException {
  51. Configuration conf = context.getConfiguration();
  52. String tableName = conf.get( "tableName");
  53. columnFamily = conf.get( "columnFamily");
  54. String[] qualifiers = conf.getStrings( "qualifiers");
  55. // indexes的key为列名,value为索引表名
  56. for (String q : qualifiers) {
  57. indexes.put(
  58. Bytes.toBytes(q),
  59. new ImmutableBytesWritable(Bytes.toBytes(tableName
  60. + "-" + q)));
  61. }
  62. }
  63. // super.setup(context);
  64. }
  65. public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  66. Configuration conf = HBaseConfiguration.create();
  67. String[] otherargs = new GenericOptionsParser(conf, args)
  68. .getRemainingArgs(); // 去除掉没有用的命令行参数
  69. // 输入参数:表名,列族名,列名
  70. if (otherargs.length < 3) {
  71. System.exit(- 1);
  72. }
  73. String tableName = otherargs[ 0];
  74. String columnFamily = otherargs[ 1];
  75. conf.set( "tableName", tableName);
  76. conf.set( "columnFamily", columnFamily);
  77. String[] qualifiers = new String[otherargs.length - 2];
  78. for ( int i = 0; i < qualifiers.length; i++) {
  79. qualifiers[i] = otherargs[i + 2];
  80. }
  81. conf.setStrings( "qualifiers", qualifiers);
  82. Job job = new Job(conf, tableName);
  83. job.setJarByClass(MyIndexBuilder.class);
  84. job.setMapperClass(MyIndexMapper.class);
  85. job.setNumReduceTasks( 0);
  86. job.setInputFormatClass(TableInputFormat.class);
  87. // 可以输出多张表
  88. job.setOutputFormatClass(MultiTableOutputFormat.class);
  89. Scan scan = new Scan();
  90. scan.setCaching( 1000);
  91. TableMapReduceUtil.initTableMapperJob(tableName, scan, MyIndexMapper.class,
  92. ImmutableBytesWritable.class, Put.class, job);
  93. job.waitForCompletion( true);
  94. }
  95. }

 

 

 

HBase 协处理器(coprocessor)实现二级索引

HBase在0.92之后引入了coprocessors,提供了一系列的钩子,让我们能够轻易实现访问控制和二级索引的特性。

HBase Coprocessor简介

•HBase Coprocessor受启发于Google的Jeff Dean在LADIS’09 上的报告

–Google BigTable的Coprocessor特点

•在每个表服务器的任何tablet上均可执行用户代码

•提供客户端调用接口 (coprocessor客户端lib将可定位每个row/range的位置;多行读写将自

动分片为多个并行的RPC调用)

•提供可构建分布式服务的灵活的编程模型

•可以自动扩展,负载均衡等

–与Google Bigtable Coprocessor相比

•Bigtable coprocessor 以独立的进程执行,可以更好的控制CP计算所需资源

•HBase coprocessor是一个在Master/RegionServer进程内的框架,通过在运行时执行用户的代码,在HBase内实现灵活的分布式数据处理功能

•HBase Coprocessor的主要应用场景

–secondary indexing

–complex filtering

–access control

HBase Coprocessor 的实现类型

•HBase Coprocessor的实现分为Observer和Endpoint两种

–Observer类似于触发器,工作在服务器端。可以实现权限管理、监控等

–Endpoint类似于存储过程,工作在服务器端和客户端。可以实现min/max等计算

•Coprocessor的作用范围

–System coprocessor: 对所有table的所有region

–Table coprocessor:对某个table的所有region

•RegionObserver:提供表数据操作事件的钩子函数:Get、Put、Scan等的pre/post处理。

•WALObserver:提供WAL相关操作钩子。

•MasterObserver:提供DDL类型的操作钩子。如创建、删除、修改数据表等。

Endpoint:只适用于RegionServer, 对应于每个table 的Region的处理。

想要更详细的介绍请查阅:

https://blogs.apache.org/hbase/entry/coprocessor_introduction

observers分为三种:

RegionObserver:提供数据操作事件钩子;

WALObserver:提供WAL(write ahead log)相关操作事件钩子;

MasterObserver:提供DDL操作事件钩子。

实例

该例子使用RegionObserver实现在写主表之前将索引数据先写到另外一个表


 
 
  1. import org.apache.hadoop.conf.Configuration;
  2. import org.apache.hadoop.hbase.Cell;
  3. import org.apache.hadoop.hbase.KeyValue;
  4. import org.apache.hadoop.hbase.client.Durability;
  5. import org.apache.hadoop.hbase.client.HTable;
  6. import org.apache.hadoop.hbase.client.Put;
  7. import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
  8. import org.apache.hadoop.hbase.coprocessor.ObserverContext;
  9. import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
  10. import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
  11. import java.io.IOException;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. public class IndexHBaseCoprocessor extends BaseRegionObserver {
  15. @Override
  16. public void prePut(ObserverContext<RegionCoprocessorEnvironment> e, Put put, WALEdit edit, Durability durability) throws IOException {
  17. //set configuration
  18. Configuration conf = new Configuration();
  19. //need conf.set...
  20. HTable table = new HTable(conf, "indexTableName");
  21. List<Cell> kv = put.get( "familyName".getBytes(), "columnName".getBytes());
  22. Iterator<Cell> kvItor = kv.iterator();
  23. while (kvItor.hasNext()) {
  24. Cell tmp = kvItor.next();
  25. final byte[] value = tmp.getValue();
  26. Put indexPut = new Put(value);
  27. indexPut.add( "familyName".getBytes(), "columnName".getBytes(), tmp.getRow());
  28. table.put(indexPut);
  29. }
  30. table.close();
  31. // super.prePut(e, put, edit, durability);
  32. }
  33. }
  34. 这是类之间的继承关系和实现里面的方法:
  35. public class IndexHBaseCoprocessor extends BaseRegionObserver {
  36. public class BaseRegionObserver implements RegionObserver {
  37. public interface RegionObserver extends Coprocessor {
  38. void prePut(ObserverContext<RegionCoprocessorEnvironment> var1, Put var2, WALEdit var3, Durability var4) throws IOException;
  39. }

 

写完后要加载到table里面去,先把该文件打包indexTest.jar并上传到hdfs的/hbase-test路径下,然后操作如下:

 

进入hbase shell ,执行一下命令行:

 

1. disable ‘testTable’

2.alter ‘testTable’,

METHOD=>’table_att’,’coprocessor’=>’hdfs:///hbase-test/indexTest.jar|com.hbase

.IndexHBaseCoprocessor|1001′

enable ‘testTable’

然后往testTable里面插数据就会自动往indexTableName写数据了。

这就是用coprocessor实现二级索引的例子。

 

HBase  IndexBuilder.java源码

链接:https://pan.baidu.com/s/140ZTLE-pFJZXeMRo6QQuNg  密码:ql9d

 

参考博文:

1.http://www.aboutyun.com/forum.php?mod=viewthread&tid=8857&highlight=hbase%2B%B6%FE%BC%B6

2.https://www.cnblogs.com/MOBIN/p/5579088.html

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值