001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.mapred.lib;
019
020import org.apache.hadoop.classification.InterfaceAudience;
021import org.apache.hadoop.classification.InterfaceStability;
022import org.apache.hadoop.mapred.JobConf;
023import org.apache.hadoop.mapred.Mapper;
024import org.apache.hadoop.mapred.OutputCollector;
025import org.apache.hadoop.mapred.Reporter;
026
027import java.io.IOException;
028
029/**
030 * The ChainMapper class allows to use multiple Mapper classes within a single
031 * Map task.
032 * <p>
033 * The Mapper classes are invoked in a chained (or piped) fashion, the output of
034 * the first becomes the input of the second, and so on until the last Mapper,
035 * the output of the last Mapper will be written to the task's output.
036 * <p>
037 * The key functionality of this feature is that the Mappers in the chain do not
038 * need to be aware that they are executed in a chain. This enables having
039 * reusable specialized Mappers that can be combined to perform composite
040 * operations within a single task.
041 * <p>
042 * Special care has to be taken when creating chains that the key/values output
043 * by a Mapper are valid for the following Mapper in the chain. It is assumed
044 * all Mappers and the Reduce in the chain use maching output and input key and
045 * value classes as no conversion is done by the chaining code.
046 * <p>
047 * Using the ChainMapper and the ChainReducer classes is possible to compose
048 * Map/Reduce jobs that look like <code>[MAP+ / REDUCE MAP*]</code>. And
049 * immediate benefit of this pattern is a dramatic reduction in disk IO.
050 * <p>
051 * IMPORTANT: There is no need to specify the output key/value classes for the
052 * ChainMapper, this is done by the addMapper for the last mapper in the chain.
053 * <p>
054 * ChainMapper usage pattern:
055 * <p>
056 * <pre>
057 * ...
058 * conf.setJobName("chain");
059 * conf.setInputFormat(TextInputFormat.class);
060 * conf.setOutputFormat(TextOutputFormat.class);
061 *
062 * JobConf mapAConf = new JobConf(false);
063 * ...
064 * ChainMapper.addMapper(conf, AMap.class, LongWritable.class, Text.class,
065 *   Text.class, Text.class, true, mapAConf);
066 *
067 * JobConf mapBConf = new JobConf(false);
068 * ...
069 * ChainMapper.addMapper(conf, BMap.class, Text.class, Text.class,
070 *   LongWritable.class, Text.class, false, mapBConf);
071 *
072 * JobConf reduceConf = new JobConf(false);
073 * ...
074 * ChainReducer.setReducer(conf, XReduce.class, LongWritable.class, Text.class,
075 *   Text.class, Text.class, true, reduceConf);
076 *
077 * ChainReducer.addMapper(conf, CMap.class, Text.class, Text.class,
078 *   LongWritable.class, Text.class, false, null);
079 *
080 * ChainReducer.addMapper(conf, DMap.class, LongWritable.class, Text.class,
081 *   LongWritable.class, LongWritable.class, true, null);
082 *
083 * FileInputFormat.setInputPaths(conf, inDir);
084 * FileOutputFormat.setOutputPath(conf, outDir);
085 * ...
086 *
087 * JobClient jc = new JobClient(conf);
088 * RunningJob job = jc.submitJob(conf);
089 * ...
090 * </pre>
091 */
092@InterfaceAudience.Public
093@InterfaceStability.Stable
094public class ChainMapper implements Mapper {
095
096  /**
097   * Adds a Mapper class to the chain job's JobConf.
098   * <p>
099   * It has to be specified how key and values are passed from one element of
100   * the chain to the next, by value or by reference. If a Mapper leverages the
101   * assumed semantics that the key and values are not modified by the collector
102   * 'by value' must be used. If the Mapper does not expect this semantics, as
103   * an optimization to avoid serialization and deserialization 'by reference'
104   * can be used.
105   * <p>
106   * For the added Mapper the configuration given for it,
107   * <code>mapperConf</code>, have precedence over the job's JobConf. This
108   * precedence is in effect when the task is running.
109   * <p>
110   * IMPORTANT: There is no need to specify the output key/value classes for the
111   * ChainMapper, this is done by the addMapper for the last mapper in the chain
112   * <p>
113   *
114   * @param job              job's JobConf to add the Mapper class.
115   * @param klass            the Mapper class to add.
116   * @param inputKeyClass    mapper input key class.
117   * @param inputValueClass  mapper input value class.
118   * @param outputKeyClass   mapper output key class.
119   * @param outputValueClass mapper output value class.
120   * @param byValue          indicates if key/values should be passed by value
121   * to the next Mapper in the chain, if any.
122   * @param mapperConf       a JobConf with the configuration for the Mapper
123   * class. It is recommended to use a JobConf without default values using the
124   * <code>JobConf(boolean loadDefaults)</code> constructor with FALSE.
125   */
126  public static <K1, V1, K2, V2> void addMapper(JobConf job,
127                           Class<? extends Mapper<K1, V1, K2, V2>> klass,
128                           Class<? extends K1> inputKeyClass,
129                           Class<? extends V1> inputValueClass,
130                           Class<? extends K2> outputKeyClass,
131                           Class<? extends V2> outputValueClass,
132                           boolean byValue, JobConf mapperConf) {
133    job.setMapperClass(ChainMapper.class);
134    job.setMapOutputKeyClass(outputKeyClass);
135    job.setMapOutputValueClass(outputValueClass);
136    Chain.addMapper(true, job, klass, inputKeyClass, inputValueClass,
137                    outputKeyClass, outputValueClass, byValue, mapperConf);
138  }
139
140  private Chain chain;
141
142  /**
143   * Constructor.
144   */
145  public ChainMapper() {
146    chain = new Chain(true);
147  }
148
149  /**
150   * Configures the ChainMapper and all the Mappers in the chain.
151   * <p>
152   * If this method is overriden <code>super.configure(...)</code> should be
153   * invoked at the beginning of the overwriter method.
154   */
155  public void configure(JobConf job) {
156    chain.configure(job);
157  }
158
159  /**
160   * Chains the <code>map(...)</code> methods of the Mappers in the chain.
161   */
162  @SuppressWarnings({"unchecked"})
163  public void map(Object key, Object value, OutputCollector output,
164                  Reporter reporter) throws IOException {
165    Mapper mapper = chain.getFirstMap();
166    if (mapper != null) {
167      mapper.map(key, value, chain.getMapperCollector(0, output, reporter),
168                 reporter);
169    }
170  }
171
172  /**
173   * Closes  the ChainMapper and all the Mappers in the chain.
174   * <p>
175   * If this method is overriden <code>super.close()</code> should be
176   * invoked at the end of the overwriter method.
177   */
178  public void close() throws IOException {
179    chain.close();
180  }
181
182}