单词计数:Hadoop Word Count
实现代码:
import java.util.*;
/**
* 单词计数
*/
public class WordCount {
private interface OutputCollector<K, V> {
void collect(K key, V value);
}
public static class Map {
public void map(String key, String value, OutputCollector<String, Integer> output) {
StringTokenizer tokenizer = new StringTokenizer(value);
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
output.collect(word, 1);
}
}
}
public static class Reduce {
public void reduce(String key, Iterator<Integer> values, OutputCollector<String, Integer> output) {
int sum = 0;
while (values.hasNext()) {
sum += values.next();
}
output.collect(key, sum);
}
}
}