5.9
其实并没有理解这个题目是什么意思。后来还是参考了别人的答案。
首先使用map,把单词都提取出来,建立<key,1>的映射关系。
然后使用reduce,把key值相同的进行计数。
大概就是这个意思,其实我还是不明白,这题是在干什么。
/**
* 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 {
public static class Map {
public void map(String key, String value, OutputCollector<String, Integer> output) {
// Write your code here
// Output the results into output buffer.
// Ps. output.collect(String key, int value);
String[] tmp = value.split(" ");
for(int i = 0;i < tmp.length;i++){
output.collect(tmp[i],1);
}
}
}
public static class Reduce {
public void reduce(String key, Iterator<Integer> values,
OutputCollector<String, Integer> output) {
// Write your code here
// Output the results into output buffer.
// Ps. output.collect(String key, int value);
int count = 0;
while(values.hasNext()){
count += values.next();
}
output.collect(key , count);
}
}
}