WordCountReducer.java:
package com.imd.examples;
import java.io.IOException;
import java.util.Iterator;
import org.apache.Hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reducer;
import org.apache.hadoop.mapred.Reporter;
public class WordCountReducer extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
@Override
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
Integer sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
然后compile:
javac -classpath /home/junhaozhang/hadoop-0.23.9/share/hadoop/mapreduce/hadoop-mapreduce-client-core-0.23.9.jar:/home/junhaozhang/hadoop-0.23.9/share/hadoop/common/hadoop-common-0.23.9.jar *.java -d .
jar -cvf WordCount2.jar com
运行:
hadoop jar WordCount2.jar com.imd.examples.WordCount2 releases.html output
output下看到结果了。
下面就需要尝试下伪分布式和分布式了。
更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13
