Map类负责对原始数据进行处理,将单词拆分后输出到output; Reduce负责对Map输出的数据进行计数。
C++代码:
/**
* Definition of Input:
* template<class T>
* class Input {
* public:
* bool done();
* // Returns true if the iteration has elements or false.
* void next();
* // Move to the next element in the iteration
* // Runtime error if the iteration has no more elements
* T value();
* // Get the current element, Runtime error if
* // the iteration has no more elements
* }
*/
class WordCountMapper: public Mapper {
public:
void Map(Input<string>* input) {
// Write your code here
// Please directly use func 'output' to
// output the results into output buffer.
// void output(string &key, int value);
vector<string> vecStr;
while(!input->done()){
string str = input->value();
int j =0;
for(int i = 0; i<=str.size(); i++){
if (str[i] == ' '||i == str.size()){
string temp = str.substr(j,i-j);
output(temp,1);
j = i+1;
}
}
input->next();
}
}
};
class WordCountReducer: public Reducer {
public:
void Reduce(string &key, Input<int>* input) {
// Write your code here
// Please directly use func 'output' to
// output the results into output buffer.
// void output(string &key, int value);
int sum = 0;
while(!input->done()) {
sum += input->value();
input->next();
}
output(key, sum);
}
};