用户自定义的RecordWriter类需要继承虚基类HadoopPipes:: RecordWriter,它的声明如下:
- class RecordWriter: public Closable {
- public:
- virtual void emit(const std::string& key,
- const std::string& value) = 0;
- };
- class MyWriter: public HadoopPipes::RecordWriter {
- public:
- MyWriter(HadoopPipes::ReduceContext& context) {
- const HadoopPipes::JobConf* job = context.getJobConf();
- int part = job->getInt("mapred.task.partition");
- std::string outDir = job->get("mapred.work.output.dir");
- ……
- }
- }
4. Hadoop pipes编程示例
网上有很多人怀疑Hadoop pipes自带的程序wordcount-nopipe.cc不能运行,各个论坛都有讨论,在此介绍该程序的设计原理和运行方法。(事实证明是可以用的至少0.20.2这个版本的是可以用的)
该运行需要具备以下前提:
(1) 采用的InputFormat为WordCountInputFormat,它位于src/test/下的org.apache.hadoop.mapred.pipes中
(2) 输入目录和输出目录需位于各个datanode的本地磁盘上,格式为:file:///home/xxx/pipes_test (注意,hdfs中的各种接口同时支持本地路径和HDFS路径,如果是HDFS上的路径,需要使用hdfs://host:9000/user/xxx,表示/user/xxx为namenode 为host的hdfs上的路径,而本地路径,需使用file:///home/xxx/pipes_test,表示/home/xxx/pipes_test为本地路径。例如,bin/hadoop fs –ls file:///home/xxx/pipes_test表示列出本地磁盘上/home/xxx/pipes_tes下的文件)
待确定好各个datanode的本地磁盘上有输入数据/home/xxx/pipes_test/data.txt后,用户首先上传可执行文件到HDFS中:
1
bin/hadoop -put build/c++-examples/Linux-amd64-64/bin/wordcount-nopipe bin/
然后使用下面命令提交该作业:
- bin/hadoop pipes \
- -D hadoop.pipes.java.recordreader=false \
- -D hadoop.pipes.java.recordwriter=false \
- -D mapred.job.name=wordcount \
- -D mapred.input.format.class=org.apache.hadoop.mapred.pipes.WordCountInputFormat \
- -libjars hadoop-0.20.2-test.jar \
- -input file:///home/xxx/pipes_test/data.txt \
- -output file:///home/xxx/pipes_output \
- -program /user/XXX/bin/wordcount-nopipe
如果用户需要在mapreduce作业中加载词典或者传递参数,可这样做:
(1) 提交作业时,用-files选项,将词典(需要传递参数可以放到一个配置文件中)上传给各个datanode,如:
- bin/hadoop pipes \
- -D hadoop.pipes.java.recordreader=false \
- -D hadoop.pipes.java.recordwriter=false \
- -D mapred.job.name=wordcount \
- -files dic.txt \
- ….
WordCountMap(HadoopPipes::TaskContext& context) {
file = fopen(“dic.txt”, "r"); //C库函数
…….
}
为了提高系能,RecordReader和RecordWriter最好采用Java代码实现(或者重用Hadoop中自带的,),这是因为Hadoop自带的C++库libhdfs采用JNI实现,底层还是要调用Java相关接口,效率很低,此外,如果要处理的文件为二进制文件或者其他非文本文件,libhdfs可能不好处理。
但是这一部分如何实现呢,如何在程序中指定对应的reader和writer类呢?
查看了一下hadoop pipes命令的使用格式:
bin/hadoop pipes
[-input <path>] // Input directory
[-output <path>] // Output directory
[-jar <jar file> // jar filename
[-inputformat <class>] // InputFormat class
[-map <class>] // Java Map class
[-partitioner <class>] // Java Partitioner
[-reduce <class>] // Java Reduce class
[-writer <class>] // Java RecordWriter
[-program <executable>] // executable URI
[-reduces <num>] // number of reduces
Generic options supported are
-conf <configuration file> specify an application configuration file
-D <property=value> use value for given property
-fs <local|namenode:port> specify a namenode
-jt <local|jobtracker:port> specify a job tracker
-files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars> specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines.
发现有-writer,但是没有-reader这个是怎么回事啊?难道 inputformat就可以了,这两个是不同的啊?如果有人知道,麻烦留言告诉我一声啊
6. 总结
Hadoop pipes使C++程序员编写MapReduce作业变得可能,它简单好用,提供了用户所需的大部分功能。
