Hadoop数单词个数实例

public class MyMap extends MapReduceBase implements Mapper {
	Text t = new Text();
	private final static IntWritable one = new IntWritable(1);
	private Text word = new Text();
	@Override
	public void map(WritableComparable key,  //分发过程
			Writable value, OutputCollector output,
			Reporter reporter) throws IOException {
		String line = value.toString();   
		StringTokenizer stz = new StringTokenizer(line);
		while(stz.hasMoreTokens()){
			word.set(stz.nextToken());  //设置输出的key
			output.collect(word, one);  //输出到shuffle过程
		}
	}
}
public class MyReduce extends MapReduceBase implements Reducer {   //收缩过程
  
    public void reduce(WritableComparable key, Iterator values, OutputCollector output,Reporter reporter) throws IOException {  
            int sum = 0;  
            while(values.hasNext()){  
                sum+=Integer.parseInt(values.next().toString());     //将从shuffle里读入的键值对进行求和
            }  
            output.collect(key, new IntWritable(sum));   //将结果输出到终端
    }  
}  


你可能感兴趣的:(Hadoop数单词个数实例)