hadoop提供了java版本的mapreduce编程API,我们需要自定义编写mapper和reducer,分别继承Mapper和Reducer,然后重写map和reduce方法。同时需要在main方法中构建job,然后指定mapper和reducer,最后提交任务。同时也支持c++编写mapreduce。hadoop有几种方式用c++实现mapreduce,这里介绍使用hadoop-streaming-xxx.jar的方式来运行c++实现的mapreduce程序。
我们需要定义两个c++文件,分别编写mapper和reducer执行的任务。这里以词频统计为例,在mapper中,我们需要实现的是<word,1>这样的输出。在reducer中,我们需要通过统计的方式来计算word出现的总次数,输出<word,sum>。
下面给出源代码:
mapper.cpp
#include <iostream>
#include <string>
using namespace std;
int main(){
string word;
while(cin>>word){
cout<<word<<"\t"<<"1"<<endl;
}
return 0;
}
reducer.cpp
#include <iostream>
#include <string>
#include <map>
using namespace std;
int ma