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 019package org.apache.hadoop.mapreduce.lib.aggregate; 020 021import java.io.IOException; 022import java.util.Iterator; 023import java.util.Map.Entry; 024 025import org.apache.hadoop.classification.InterfaceAudience; 026import org.apache.hadoop.classification.InterfaceStability; 027import org.apache.hadoop.io.Text; 028import org.apache.hadoop.io.Writable; 029import org.apache.hadoop.io.WritableComparable; 030import org.apache.hadoop.mapreduce.Mapper; 031 032/** 033 * This class implements the generic mapper of Aggregate. 034 */ 035@InterfaceAudience.Public 036@InterfaceStability.Stable 037public class ValueAggregatorMapper<K1 extends WritableComparable<?>, 038 V1 extends Writable> 039 extends Mapper<K1, V1, Text, Text> { 040 041 public void setup(Context context) 042 throws IOException, InterruptedException { 043 ValueAggregatorJobBase.setup(context.getConfiguration()); 044 } 045 046 /** 047 * the map function. It iterates through the value aggregator descriptor 048 * list to generate aggregation id/value pairs and emit them. 049 */ 050 public void map(K1 key, V1 value, 051 Context context) throws IOException, InterruptedException { 052 053 Iterator<?> iter = 054 ValueAggregatorJobBase.aggregatorDescriptorList.iterator(); 055 while (iter.hasNext()) { 056 ValueAggregatorDescriptor ad = (ValueAggregatorDescriptor) iter.next(); 057 Iterator<Entry<Text, Text>> ens = 058 ad.generateKeyValPairs(key, value).iterator(); 059 while (ens.hasNext()) { 060 Entry<Text, Text> en = ens.next(); 061 context.write(en.getKey(), en.getValue()); 062 } 063 } 064 } 065}