View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.mapreduce;
20  
21  import java.io.IOException;
22  import java.util.TreeMap;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.client.Put;
27  import org.apache.hadoop.hbase.client.Result;
28  import org.apache.hadoop.hbase.client.Scan;
29  import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.mapreduce.Job;
32  import org.apache.hadoop.mapreduce.Mapper;
33  import org.apache.hadoop.util.GenericOptionsParser;
34  
35  /**
36   * Example map/reduce job to construct index tables that can be used to quickly
37   * find a row based on the value of a column. It demonstrates:
38   * <ul>
39   * <li>Using TableInputFormat and TableMapReduceUtil to use an HTable as input
40   * to a map/reduce job.</li>
41   * <li>Passing values from main method to children via the configuration.</li>
42   * <li>Using MultiTableOutputFormat to output to multiple tables from a
43   * map/reduce job.</li>
44   * <li>A real use case of building a secondary index over a table.</li>
45   * </ul>
46   *
47   * <h3>Usage</h3>
48   *
49   * <p>
50   * Modify ${HADOOP_HOME}/conf/hadoop-env.sh to include the hbase jar, the
51   * zookeeper jar (can be found in lib/ directory under HBase root, the examples output directory,
52   * and the hbase conf directory in HADOOP_CLASSPATH, and then run
53   * <tt><strong>bin/hadoop org.apache.hadoop.hbase.mapreduce.IndexBuilder TABLE_NAME COLUMN_FAMILY ATTR [ATTR ...]</strong></tt>
54   * </p>
55   *
56   * <p>
57   * To run with the sample data provided in index-builder-setup.rb, use the
58   * arguments <strong><tt>people attributes name email phone</tt></strong>.
59   * </p>
60   *
61   * <p>
62   * This code was written against HBase 0.21 trunk.
63   * </p>
64   */
65  public class IndexBuilder {
66    /** the column family containing the indexed row key */
67    public static final byte[] INDEX_COLUMN = Bytes.toBytes("INDEX");
68    /** the qualifier containing the indexed row key */
69    public static final byte[] INDEX_QUALIFIER = Bytes.toBytes("ROW");
70  
71    /**
72     * Internal Mapper to be run by Hadoop.
73     */
74    public static class Map extends
75        Mapper<ImmutableBytesWritable, Result, ImmutableBytesWritable, Put> {
76      private byte[] family;
77      private TreeMap<byte[], ImmutableBytesWritable> indexes;
78  
79      @Override
80      protected void map(ImmutableBytesWritable rowKey, Result result, Context context)
81          throws IOException, InterruptedException {
82        for(java.util.Map.Entry<byte[], ImmutableBytesWritable> index : indexes.entrySet()) {
83          byte[] qualifier = index.getKey();
84          ImmutableBytesWritable tableName = index.getValue();
85          byte[] value = result.getValue(family, qualifier);
86          if (value != null) {
87            // original: row 123 attribute:phone 555-1212
88            // index: row 555-1212 INDEX:ROW 123
89            Put put = new Put(value);
90            put.add(INDEX_COLUMN, INDEX_QUALIFIER, rowKey.get());
91            context.write(tableName, put);
92          }
93        }
94      }
95  
96      @Override
97      protected void setup(Context context) throws IOException,
98          InterruptedException {
99        Configuration configuration = context.getConfiguration();
100       String tableName = configuration.get("index.tablename");
101       String[] fields = configuration.getStrings("index.fields");
102       String familyName = configuration.get("index.familyname");
103       family = Bytes.toBytes(familyName);
104       indexes = new TreeMap<byte[], ImmutableBytesWritable>(Bytes.BYTES_COMPARATOR);
105       for(String field : fields) {
106         // if the table is "people" and the field to index is "email", then the
107         // index table will be called "people-email"
108         indexes.put(Bytes.toBytes(field),
109             new ImmutableBytesWritable(Bytes.toBytes(tableName + "-" + field)));
110       }
111     }
112   }
113 
114   /**
115    * Job configuration.
116    */
117   public static Job configureJob(Configuration conf, String [] args)
118   throws IOException {
119     String tableName = args[0];
120     String columnFamily = args[1];
121     System.out.println("****" + tableName);
122     conf.set(TableInputFormat.SCAN, TableMapReduceUtil.convertScanToString(new Scan()));
123     conf.set(TableInputFormat.INPUT_TABLE, tableName);
124     conf.set("index.tablename", tableName);
125     conf.set("index.familyname", columnFamily);
126     String[] fields = new String[args.length - 2];
127     System.arraycopy(args, 2, fields, 0, fields.length);
128     conf.setStrings("index.fields", fields);
129     Job job = new Job(conf, tableName);
130     job.setJarByClass(IndexBuilder.class);
131     job.setMapperClass(Map.class);
132     job.setNumReduceTasks(0);
133     job.setInputFormatClass(TableInputFormat.class);
134     job.setOutputFormatClass(MultiTableOutputFormat.class);
135     return job;
136   }
137 
138   public static void main(String[] args) throws Exception {
139     Configuration conf = HBaseConfiguration.create();
140     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
141     if(otherArgs.length < 3) {
142       System.err.println("Only " + otherArgs.length + " arguments supplied, required: 3");
143       System.err.println("Usage: IndexBuilder <TABLE_NAME> <COLUMN_FAMILY> <ATTR> [<ATTR> ...]");
144       System.exit(-1);
145     }
146     Job job = configureJob(conf, otherArgs);
147     System.exit(job.waitForCompletion(true) ? 0 : 1);
148   }
149 }