hbase二级索引构建

参考学习hbase源代码中的二级索引构建代码

IndexBuilder.java

/**
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.hadoop.hbase.mapreduce;

import java.io.IOException;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.util.GenericOptionsParser;

/**
 * Example map/reduce job to construct index tables that can be used to quickly
 * find a row based on the value of a column. It demonstrates:
 * <ul>
 * <li>Using TableInputFormat and TableMapReduceUtil to use an HTable as input
 * to a map/reduce job.</li>
 * <li>Passing values from main method to children via the configuration.</li>
 * <li>Using MultiTableOutputFormat to output to multiple tables from a
 * map/reduce job.</li>
 * <li>A real use case of building a secondary index over a table.</li>
 * </ul>
 *
 * <h3>Usage</h3>
 *
 * <p>
 * Modify ${HADOOP_HOME}/conf/hadoop-env.sh to include the hbase jar, the
 * zookeeper jar (can be found in lib/ directory under HBase root, the examples output directory,
 * and the hbase conf directory in HADOOP_CLASSPATH, and then run
 * <tt><strong>bin/hadoop org.apache.hadoop.hbase.mapreduce.IndexBuilder TABLE_NAME COLUMN_FAMILY ATTR [ATTR ...]</strong></tt>
 * </p>
 *
 * <p>
 * To run with the sample data provided in index-builder-setup.rb, use the
 * arguments <strong><tt>people attributes name email phone</tt></strong>.
 * </p>
 *
 * <p>
 * This code was written against HBase 0.21 trunk.
 * </p>
 */
public class IndexBuilder {
  /** the column family containing the indexed row key */
  public static final byte[] INDEX_COLUMN = Bytes.toBytes("INDEX");
  /** the qualifier containing the indexed row key */
  public static final byte[] INDEX_QUALIFIER = Bytes.toBytes("ROW");

  /**
   * Internal Mapper to be run by Hadoop.
   */
  public static class Map extends
      Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put> {
    private byte[] family;
    private TreeMap<byte[], ImmutableBytesWritable> indexes;

    @Override
    protected void map(ImmutableBytesWritable rowKey, Result result, Context context)
        throws IOException, InterruptedException {
      for(java.util.Map.Entry<byte[], ImmutableBytesWritable> index : indexes.entrySet()) {
        byte[] qualifier = index.getKey();
        ImmutableBytesWritable tableName = index.getValue();
        byte[] value = result.getValue(family, qualifier);
        if (value != null) {
          // original: row 123 attribute:phone 555-1212
          // index: row 555-1212 INDEX:ROW 123
          Put put = new Put(value);
          put.add(INDEX_COLUMN, INDEX_QUALIFIER, rowKey.get());
          context.write(tableName, put);
        }
      }
    }

    @Override
    protected void setup(Context context) throws IOException,
        InterruptedException {
      Configuration configuration = context.getConfiguration();
      String tableName = configuration.get("index.tablename");
      String[] fields = configuration.getStrings("index.fields");
      String familyName = configuration.get("index.familyname");
      family = Bytes.toBytes(familyName);
      indexes = new TreeMap<byte[], ImmutableBytesWritable>(Bytes.BYTES_COMPARATOR);
      for(String field : fields) {
        // if the table is "people" and the field to index is "email", then the
        // index table will be called "people-email"
        indexes.put(Bytes.toBytes(field),
            new ImmutableBytesWritable(Bytes.toBytes(tableName + "-" + field)));
      }
    }
  }

  /**
   * Job configuration.
   */
  public static Job configureJob(Configuration conf, String [] args)
  throws IOException {
    String tableName = args[0];
    String columnFamily = args[1];
    System.out.println("****" + tableName);
    //这个会报函数无权限,注释掉    //conf.set(TableInputFormat.SCAN, TableMapReduceUtil.convertScanToString(new Scan()));
    conf.set(TableInputFormat.INPUT_TABLE, tableName);
    conf.set("index.tablename", tableName);
    conf.set("index.familyname", columnFamily);
    String[] fields = new String[args.length - 2];
    System.arraycopy(args, 2, fields, 0, fields.length);
    conf.setStrings("index.fields", fields);
    Job job = new Job(conf, tableName);
    job.setJarByClass(IndexBuilder.class);
    job.setMapperClass(Map.class);
    job.setNumReduceTasks(0);
    job.setInputFormatClass(TableInputFormat.class);
    job.setOutputFormatClass(MultiTableOutputFormat.class);
    return job;
  }

  public static void main(String[] args) throws Exception {
    Configuration conf = HBaseConfiguration.create();
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    if(otherArgs.length < 3) {
      System.err.println("Only " + otherArgs.length + " arguments supplied, required: 3");
      System.err.println("Usage: IndexBuilder <TABLE_NAME> <COLUMN_FAMILY> <ATTR> [<ATTR> ...]");
      System.exit(-1);
    }
    Job job = configureJob(conf, otherArgs);
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}
时间: 2024-11-05 23:59:35

hbase二级索引构建的相关文章

HBase二级索引的设计

摘要 最近做的一个项目涉及到了多条件的组合查询,数据存储用的是HBase,恰恰HBase对于这种场景的查询特别不给力,一般HBase的查询都是通过RowKey(要把多条件组合查询的字段都拼接在RowKey中显然不太可能),或者全表扫描再结合过滤器筛选出目标数据(太低效),所以通过设计HBase的二级索引来解决这个问题 查询需求 多个查询条件构成多维度的组合查询,需要根据不同组合查询出符合查询条件的数据 HBase的局限性 HBase本身只提供基于行键和全表扫描的查询,而行键索引单一,对于多维度的

hbase二级索引

二级索引与索引Join是多数业务系统要求存储引擎提供的基本特性,RDBMS早已支持,NOSQL阵营也在摸索着符合自身特点的最佳解决方案.这篇文章会以HBase做为对象来讨论如何基于Hbase构建二级索引与实现索引join.文末同时会列出目前已知的包括0.19.3版secondary index, ITHbase, Facebook方案和官方Coprocessor的介绍. 理论目标在HBase中实现二级索引与索引Join需要考虑三个目标:1,高性能的范围检索.2,数据的低冗余(存储所占的数据量).

奇虎360 HBASE 二级索引的设计与实践

基于RowKey 的索引问题总结 1.索引单一 2.多维度(字段/列)查询困难 多字段分别作为RK,写入多次 组合字段作为RK,设计复杂,不灵活 3.不经过索引的并行scan过滤,大量资源消耗,无时效性可言 总体设计 二级索引构建模式 1)以主数据的列值作为索引数据的RowKey,以主数据的RowKey 作为索引数据的列值,以此来构建指定列作为查询条件的Hbase 二级索引. 2)索引的构建与数据的查询都是分布式.并发式进行的 索引设计 1)索引与主数据存放在同一张表的不同Column Fami

HBase二级索引方案总结

转自:http://blog.sina.com.cn/s/blog_4a1f59bf01018apd.html 附hbase如何创建二级索引以及创建二级索引实例:http://www.aboutyun.com/thread-8857-1-1.html 华为二级索引(原理):http://my.oschina.net/u/923508/blog/413129 在HBase中,表格的Rowkey按照字典排序,Region按照RowKey设置split point进行shard,通过这种方式实现的全局

CDH Solr Hbase二级索引

基于key-Value store indexer ,solrcloud创建Hbase二级索引 首先安装solrcloud,在cloudera manager 上添加solr服务,然后添加key-Value store indexer服务. 首先设置HBASE表的列族REPLICATION_SCOP =>1 如: disable ‘cloud’ alter 'cloud' ,{NAME => 'datainfo',REPLICATION_SCOPE =>'1'} enable ‘clou

【转】华为HBase索引模块应用:HBase二级索引模块:hindex调研 2014年10月16日

文章出处:http://www.batchfile.cn/?p=63 HBase二级索引模块:hindex调研 hindx是HBase的二级索引方案,为HBase提供声明式的索引,使用协处理器对索引表进行自动创建和维护,客户端不需要对数据进行双写.并且hindex采用了一些巧妙的Rowkey编排方式,使索引数据和实际数据分布在同一个Region,实现了较高的查询性能.介绍如下:huawei-hbase-secondary-secondary-index-implementations 代码下载地

HBase二级索引与Join

转自:http://www.oschina.net/question/12_32573 二级索引与索引Join是Online业务系统要求存储引擎提供的基本特性.RDBMS支持得比较好,NOSQL阵营也在摸索着符合自身特点的最佳解决方案.这篇文章会以HBase做为对象来探讨如何基于Hbase构建二级索引与实现索引join.文末同时会列出目前已知的包括0.19.3版secondary index, ITHbase, Facebook和官方Coprocessor方案的介绍. 理论目标在HBase中实现

(转)HBase二级索引与Join

二级索引与索引Join是Online业务系统要求存储引擎提供的基本特性.RDBMS支持得比较好,NOSQL阵营也在摸索着符合自身特点的最佳解决方案.这篇文章会以HBase做为对象来探讨如何基于Hbase构建二级索引与实现索引join.文末同时会列出目前已知的包括0.19.3版secondary index,?ITHbase, Facebook和官方Coprocessor方案的介绍. 理论目标在HBase中实现二级索引与索引Join需要考虑三个目标:1,高性能的范围检索.2,数据的低冗余(存储所占

HBase 二级索引和备用查询路径

感谢平台分享-http://bjbsair.com/2020-04-10/tech-info/53319.html 你也可以将本文的标题理解为"如果我的表 rowkey 看起来像这样,但我也希望我的查询表这样." dist-list 上的一个常见示例是 row-key 格式为"user-timestamp"格式,但对于特定时间范围内的用户活动有报告要求.因此,用户选择容易,因为它处于密钥的主导位置,但时间不是. 没有一个最好的方法来解决这个问题的答案,因为它取决于: