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     */
018    package org.apache.hadoop.mapred.lib;
019    
020    import org.apache.hadoop.classification.InterfaceAudience;
021    import org.apache.hadoop.classification.InterfaceStability;
022    import org.apache.hadoop.mapred.JobConf;
023    import org.apache.hadoop.mapred.Mapper;
024    import org.apache.hadoop.mapred.OutputCollector;
025    import org.apache.hadoop.mapred.Reporter;
026    
027    import 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     * <p/>
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     * <p/>
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     * <p/>
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     * <p/>
077     * ChainReducer.addMapper(conf, CMap.class, Text.class, Text.class,
078     *   LongWritable.class, Text.class, false, null);
079     * <p/>
080     * ChainReducer.addMapper(conf, DMap.class, LongWritable.class, Text.class,
081     *   LongWritable.class, LongWritable.class, true, null);
082     * <p/>
083     * FileInputFormat.setInputPaths(conf, inDir);
084     * FileOutputFormat.setOutputPath(conf, outDir);
085     * ...
086     * <p/>
087     * JobClient jc = new JobClient(conf);
088     * RunningJob job = jc.submitJob(conf);
089     * ...
090     * </pre>
091     * @deprecated 
092     * Use {@link org.apache.hadoop.mapreduce.lib.chain.ChainMapper} instead
093     */
094    @Deprecated
095    @InterfaceAudience.Public
096    @InterfaceStability.Stable
097    public class ChainMapper implements Mapper {
098    
099      /**
100       * Adds a Mapper class to the chain job's JobConf.
101       * <p/>
102       * It has to be specified how key and values are passed from one element of
103       * the chain to the next, by value or by reference. If a Mapper leverages the
104       * assumed semantics that the key and values are not modified by the collector
105       * 'by value' must be used. If the Mapper does not expect this semantics, as
106       * an optimization to avoid serialization and deserialization 'by reference'
107       * can be used.
108       * <p/>
109       * For the added Mapper the configuration given for it,
110       * <code>mapperConf</code>, have precedence over the job's JobConf. This
111       * precedence is in effect when the task is running.
112       * <p/>
113       * IMPORTANT: There is no need to specify the output key/value classes for the
114       * ChainMapper, this is done by the addMapper for the last mapper in the chain
115       * <p/>
116       *
117       * @param job              job's JobConf to add the Mapper class.
118       * @param klass            the Mapper class to add.
119       * @param inputKeyClass    mapper input key class.
120       * @param inputValueClass  mapper input value class.
121       * @param outputKeyClass   mapper output key class.
122       * @param outputValueClass mapper output value class.
123       * @param byValue          indicates if key/values should be passed by value
124       * to the next Mapper in the chain, if any.
125       * @param mapperConf       a JobConf with the configuration for the Mapper
126       * class. It is recommended to use a JobConf without default values using the
127       * <code>JobConf(boolean loadDefaults)</code> constructor with FALSE.
128       */
129      public static <K1, V1, K2, V2> void addMapper(JobConf job,
130                               Class<? extends Mapper<K1, V1, K2, V2>> klass,
131                               Class<? extends K1> inputKeyClass,
132                               Class<? extends V1> inputValueClass,
133                               Class<? extends K2> outputKeyClass,
134                               Class<? extends V2> outputValueClass,
135                               boolean byValue, JobConf mapperConf) {
136        job.setMapperClass(ChainMapper.class);
137        job.setMapOutputKeyClass(outputKeyClass);
138        job.setMapOutputValueClass(outputValueClass);
139        Chain.addMapper(true, job, klass, inputKeyClass, inputValueClass,
140                        outputKeyClass, outputValueClass, byValue, mapperConf);
141      }
142    
143      private Chain chain;
144    
145      /**
146       * Constructor.
147       */
148      public ChainMapper() {
149        chain = new Chain(true);
150      }
151    
152      /**
153       * Configures the ChainMapper and all the Mappers in the chain.
154       * <p/>
155       * If this method is overriden <code>super.configure(...)</code> should be
156       * invoked at the beginning of the overwriter method.
157       */
158      public void configure(JobConf job) {
159        chain.configure(job);
160      }
161    
162      /**
163       * Chains the <code>map(...)</code> methods of the Mappers in the chain.
164       */
165      @SuppressWarnings({"unchecked"})
166      public void map(Object key, Object value, OutputCollector output,
167                      Reporter reporter) throws IOException {
168        Mapper mapper = chain.getFirstMap();
169        if (mapper != null) {
170          mapper.map(key, value, chain.getMapperCollector(0, output, reporter),
171                     reporter);
172        }
173      }
174    
175      /**
176       * Closes  the ChainMapper and all the Mappers in the chain.
177       * <p/>
178       * If this method is overriden <code>super.close()</code> should be
179       * invoked at the end of the overwriter method.
180       */
181      public void close() throws IOException {
182        chain.close();
183      }
184    
185    }