combiner hadoop mapreduce code example
Example: hadoop in mapper combiner
public static class Map extends Mapper<Object, Text, Text, IntWritable> {
Map count = new HashMap<Integer, Integer>();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
String token = itr.nextToken();
int length = token.length();
if(count.containsKey(length)) {
int sum = (int) count.get(length) + 1;
count.put(length, sum);
}
else {
count.put(length, 1);
}
}
}
public void cleanup(Context context) throws IOException, InterruptedException {
Iterator<Map.Entry<Integer, Integer>> temp = count.entrySet().iterator();
while(temp.hasNext()) {
Map.Entry<Integer, Integer> entry = temp.next();
String keyVal = entry.getKey()+"";
Integer countVal = entry.getValue();
context.write(new Text(keyVal), new IntWritable(countVal));
}
}
}