举个C++的统计单词个数的例子:
1. Mapper函数:
#include <iostream>
#include <string>
using namespace std;
int main()
{
}
2.reduce
#include <iostream>
#include <map>
using namespace std;
int main() {
}
3.参数意义
-file, -cacheFile, -cacheArchive:分别用于向计算节点分发本地文件(特别是当可执行文件在本地时,一定要记得指定)、HDFS文件和HDFS压缩文件。
-inputformat, -outputformat:指定inputformat和outputformat Java类,用于读取输入数据和写入输出数据,分别要实现InputFormat和OutputFormat接口。如果不指定,默认使用TextInputFormat和TextOutputFormat。
-numReduceTasks:指定reducer的个数,如果设置-numReduceTasks 0或者-reducer NONE则没有reducer程序,mapper的输出直接作为整个作业的输出。
-combiner:指定combiner Java类,对应的Java类文件打包成jar文件后用-file分发。
-partitioner:指定partitioner Java类,Streaming提供了一些实用的partitioner实现。
-cmdenv NAME=VALUE:给mapper和reducer程序传递额外的环境变量,NAME是变量名,VALUE是变量值。
4.也可以通过改变"-D<property>=<value>"来增加一些其他的配置,比如:
-D dfs.data.dir=/tmp
-D mapred.reduce.tasks=0
-D mapred.local.dir=/tmp/local
-D mapred.system.dir=/tmp/system
-D mapred.temp.dir=/tmp/temp
5. 运行
bin/hadoop fs -rmr output
bin/hadoop jar contrib/streaming/hadoop-streaming-x.y.z.jar -file Test/C++/Mapper -mapper "Test/C++/Mapper" -file Test/C++/Reducer -reducer "Test/C++/Reducer"