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    
019    package org.apache.hadoop.mapred.lib.aggregate;
020    
021    import java.io.IOException;
022    import java.util.Iterator;
023    
024    import org.apache.hadoop.classification.InterfaceAudience;
025    import org.apache.hadoop.classification.InterfaceStability;
026    import org.apache.hadoop.io.Text;
027    import org.apache.hadoop.io.Writable;
028    import org.apache.hadoop.io.WritableComparable;
029    import org.apache.hadoop.mapred.OutputCollector;
030    import org.apache.hadoop.mapred.Reporter;
031    
032    /**
033     * This class implements the generic reducer of Aggregate.
034     */
035    @InterfaceAudience.Public
036    @InterfaceStability.Stable
037    public class ValueAggregatorReducer<K1 extends WritableComparable,
038                                        V1 extends Writable>
039      extends ValueAggregatorJobBase<K1, V1> {
040    
041      /**
042       * @param key
043       *          the key is expected to be a Text object, whose prefix indicates
044       *          the type of aggregation to aggregate the values. In effect, data
045       *          driven computing is achieved. It is assumed that each aggregator's
046       *          getReport method emits appropriate output for the aggregator. This
047       *          may be further customiized.
048       * @value the values to be aggregated
049       */
050      public void reduce(Text key, Iterator<Text> values,
051                         OutputCollector<Text, Text> output, Reporter reporter) throws IOException {
052        String keyStr = key.toString();
053        int pos = keyStr.indexOf(ValueAggregatorDescriptor.TYPE_SEPARATOR);
054        String type = keyStr.substring(0, pos);
055        keyStr = keyStr.substring(pos
056                                  + ValueAggregatorDescriptor.TYPE_SEPARATOR.length());
057    
058        ValueAggregator aggregator = ValueAggregatorBaseDescriptor
059          .generateValueAggregator(type);
060        while (values.hasNext()) {
061          aggregator.addNextValue(values.next());
062        }
063    
064        String val = aggregator.getReport();
065        key = new Text(keyStr);
066        output.collect(key, new Text(val));
067      }
068    
069      /**
070       * Do nothing. Should not be called
071       */
072      public void map(K1 arg0, V1 arg1, OutputCollector<Text, Text> arg2,
073                      Reporter arg3) throws IOException {
074        throw new IOException ("should not be called\n");
075      }
076    }