source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/fs/AccumulatingReducer.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.4 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.fs;
19
20import java.io.IOException;
21import java.util.Iterator;
22
23import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25import org.apache.hadoop.io.UTF8;
26import org.apache.hadoop.io.WritableComparable;
27import org.apache.hadoop.mapred.MapReduceBase;
28import org.apache.hadoop.mapred.OutputCollector;
29import org.apache.hadoop.mapred.Reducer;
30import org.apache.hadoop.mapred.Reporter;
31
32/**
33 * Reducer that accumulates values based on their type.
34 * <p>
35 * The type is specified in the key part of the key-value pair
36 * as a prefix to the key in the following way
37 * <p>
38 * <tt>type:key</tt>
39 * <p>
40 * The values are accumulated according to the types:
41 * <ul>
42 * <li><tt>s:</tt> - string, concatenate</li>
43 * <li><tt>f:</tt> - float, summ</li>
44 * <li><tt>l:</tt> - long, summ</li>
45 * </ul>
46 *
47 */
48public class AccumulatingReducer extends MapReduceBase
49    implements Reducer<UTF8, UTF8, UTF8, UTF8> {
50  private static final Log LOG = LogFactory.getLog(AccumulatingReducer.class);
51 
52  protected String hostName;
53 
54  public AccumulatingReducer () {
55    LOG.info("Starting AccumulatingReducer !!!");
56    try {
57      hostName = java.net.InetAddress.getLocalHost().getHostName();
58    } catch(Exception e) {
59      hostName = "localhost";
60    }
61    LOG.info("Starting AccumulatingReducer on " + hostName);
62  }
63 
64  public void reduce(UTF8 key, 
65                     Iterator<UTF8> values,
66                     OutputCollector<UTF8, UTF8> output, 
67                     Reporter reporter
68                     ) throws IOException {
69    String field = key.toString();
70
71    reporter.setStatus("starting " + field + " ::host = " + hostName);
72
73    // concatenate strings
74    if (field.startsWith("s:")) {
75      String sSum = "";
76      while (values.hasNext())
77        sSum += values.next().toString() + ";";
78      output.collect(key, new UTF8(sSum));
79      reporter.setStatus("finished " + field + " ::host = " + hostName);
80      return;
81    }
82    // sum long values
83    if (field.startsWith("f:")) {
84      float fSum = 0;
85      while (values.hasNext())
86        fSum += Float.parseFloat(values.next().toString());
87      output.collect(key, new UTF8(String.valueOf(fSum)));
88      reporter.setStatus("finished " + field + " ::host = " + hostName);
89      return;
90    }
91    // sum long values
92    if (field.startsWith("l:")) {
93      long lSum = 0;
94      while (values.hasNext()) {
95        lSum += Long.parseLong(values.next().toString());
96      }
97      output.collect(key, new UTF8(String.valueOf(lSum)));
98    }
99    reporter.setStatus("finished " + field + " ::host = " + hostName);
100  }
101}
Note: See TracBrowser for help on using the repository browser.