Reducer (Hadoop 1.2.1 API)
Date: 2013-07-22

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.hadoop.mapred
Interface Reducer<K2,V2,K3,V3>

All Superinterfaces:
  Closeable, JobConfigurable

All Known Implementing Classes:
  ChainReducer, DataJoinMapperBase, DataJoinReducerBase, FieldSelectionMapReduce, IdentityReducer, IndexUpdateCombiner, IndexUpdateReducer, JobBase, LongSumReducer, PiEstimator.PiReducer, PipeCombiner, PipeReducer, SleepJob, ValueAggregatorCombiner, ValueAggregatorJobBase, ValueAggregatorMapper, ValueAggregatorReducer

    public interface Reducer<K2,V2,K3,V3>

extends JobConfigurable, Closeable

Reduces a set of intermediate values which share a key to a smaller set of values.

The number of `Reducer`s for the job is set by the user via `JobConf.setNumReduceTasks(int)`. `Reducer` implementations can access the `JobConf` for the job via the `JobConfigurable.configure(JobConf)` method and initialize themselves. Similarly they can use the `Closeable.close()` method for de-initialization.

`Reducer` has 3 primary phases:

1. #### Shuffle

   `Reducer` is input the grouped output of a `Mapper`. In the phase the framework, for each `Reducer`, fetches the relevant partition of the output of all the `Mapper`s, via HTTP.

2. #### Sort

   The framework groups `Reducer` inputs by `key`s (since different `Mapper`s may have output the same key) in this stage.

   The shuffle and sort phases occur simultaneously i.e. while outputs are being fetched they are merged.

   ##### SecondarySort

   If equivalence rules for keys while grouping the intermediates are different from those for grouping keys before reduction, then one may specify a `Comparator` via `JobConf.setOutputValueGroupingComparator(Class)`.Since `JobConf.setOutputKeyComparatorClass(Class)` can be used to control how intermediate keys are grouped, these can be used in conjunction to simulate secondary sort on values.

   For example, say that you want to find duplicate web pages and tag them all with the url of the "best" known example. You would set up the job like:
   - Map Input Key: url
   - Map Input Value: document
   - Map Output Key: document checksum, url pagerank
   - Map Output Value: url
   - Partitioner: by checksum
   - OutputKeyComparator: by checksum and then decreasing pagerank
   - OutputValueGroupingComparator: by checksum

3. #### Reduce

   In this phase the `reduce(Object, Iterator, OutputCollector, Reporter)` method is called for each `<key, (list of values)>` pair in the grouped inputs.

   The output of the reduce task is typically written to the `FileSystem` via `OutputCollector.collect(Object, Object)`.

The output of the `Reducer` is not re-sorted.

Example:

>          public class MyReducer<K extends WritableComparable, V extends Writable>
>          extends MapReduceBase implements Reducer<K, V, K, V> {

>            static enum MyCounters { NUM_RECORDS }

>            private String reduceTaskId;
>            private int noKeys = 0;

>            public void configure(JobConf job) {
>              reduceTaskId = job.get("mapred.task.id");
>            }

>            public void reduce(K key, Iterator<V> values,
>                               OutputCollector<K, V> output,
>                               Reporter reporter)
>            throws IOException {

>              // Process
>              int noValues = 0;
>              while (values.hasNext()) {
>                V value = values.next();

>                // Increment the no. of values for this key
>                ++noValues;

>                // Process the <key, value> pair (assume this takes a while)
>                // ...
>                // ...

>                // Let the framework know that we are alive, and kicking!
>                if ((noValues%10) == 0) {
>                  reporter.progress();
>                }

>                // Process some more
>                // ...
>                // ...

>                // Output the <key, value>
>                output.collect(key, value);
>              }

>              // Increment the no. of <key, list of values> pairs processed
>              ++noKeys;

>              // Increment counters
>              reporter.incrCounter(NUM_RECORDS, 1);

>              // Every 100 keys update application-level status
>              if ((noKeys%100) == 0) {
>                reporter.setStatus(reduceTaskId + " processed " + noKeys);
>              }
>            }
>          }

See Also:
  `Mapper`, `Partitioner`, `Reporter`, `MapReduceBase`

Method Summary
`void` | `reduce(K2 key, Iterator<V2> values, OutputCollector<K3,V3> output, Reporter reporter)` Reduces values for a given key.

Methods inherited from interface org.apache.hadoop.mapred.JobConfigurable
`configure`

Methods inherited from interface java.io.Closeable
`close`

Method Detail

### reduce

    void reduce(K2 key,
                Iterator<V2> values,
                OutputCollector<K3,V3> output,
                Reporter reporter)
                throws IOException

  Reduces values for a given key.

  The framework calls this method for each `<key, (list of values)>` pair in the grouped inputs. Output values must be of the same type as input values. Input keys must not be altered. The framework will reuse the key and value objects that are passed into the reduce, therefore the application should clone the objects they want to keep a copy of. In many cases, all values are combined into zero or one value.

  Output pairs are collected with calls to `OutputCollector.collect(Object,Object)`.

  Applications can use the `Reporter` provided to report progress or just indicate that they are alive. In scenarios where the application takes an insignificant amount of time to process individual key/value pairs, this is crucial since the framework might assume that the task has timed-out and kill that task. The other way of avoiding this is to set mapred.task.timeout to a high-enough value (or even zero for no time-outs).

  Parameters:
    `key` - the key.
    `values` - the list of values to reduce.
    `output` - to collect keys and combined values.
    `reporter` - facility to report progress.
  Throws:
    `IOException`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2009 The Apache Software Foundation