翻转字符串
题目
使用 map reduce 来计算单词频率
https://hadoop.apache.org/docs/r1.2.1/mapred_tutorial.html#Example%3A+WordCount+v1.0样例
- ###题解###
/**
* Definition of OutputCollector:
* class OutputCollector<K, V> {
* public void collect(K key, V value);
* // Adds a key/value pair to the output buffer
* }
*/
public class WordCount {
//http://www.cnblogs.com/inevermore/p/5232534.html
public static class Map {
public void map(String key, String value, OutputCollector<String, Integer> output) {
String[] arr = value.split(" ");
for (int i=0;i<arr.length;i++)
{
output.collect(arr[i],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().intValue();
}
output.collect(key, sum);
}
}
}
Last Update 2016.9.18