手机版
你好,游客 登录 注册
背景:
阅读新闻

Hadoop pipes编程

[日期:2012-04-02] 来源:Linux社区  作者:anbo724 [字体:大 中 小]
(5) RecordWriter编写方法
用户自定义的RecordWriter类需要继承虚基类HadoopPipes:: RecordWriter,它的声明如下:
  1. class RecordWriter: public Closable {  
  2.    
  3. public:  
  4.    
  5. virtual void emit(const std::string& key,  
  6.    
  7. const std::string& value) = 0;  
  8.    
  9. };  
用户自定的RecordWriter的构造函数可携带类型为HadoopPipes::MapContext的参数,通过该参数的getJobConf()可获取一个HadoopPipes::JobConf的对象,用户可从该对象中获取该reduce task的各种参数,如:该reduce task的编号(这对于确定输出文件名有用),reduce task的输出目录等。
  1. class MyWriter: public HadoopPipes::RecordWriter {  
  2.    
  3. public:  
  4.    
  5. MyWriter(HadoopPipes::ReduceContext& context) {  
  6.    
  7. const HadoopPipes::JobConf* job = context.getJobConf();  
  8.    
  9. int part = job->getInt("mapred.task.partition");  
  10.    
  11. std::string outDir = job->get("mapred.work.output.dir");  
  12.    
  13. ……  
  14.    
  15. }  
  16.    
  17. }  
用户需实现emit方法,将数据写入某个文件。
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/
然后使用下面命令提交该作业:
  1. bin/hadoop pipes \  
  2.    
  3. -D hadoop.pipes.java.recordreader=false \  
  4.    
  5. -D hadoop.pipes.java.recordwriter=false \  
  6.    
  7. -D mapred.job.name=wordcount \  
  8.    
  9. -D mapred.input.format.class=org.apache.hadoop.mapred.pipes.WordCountInputFormat \  
  10.    
  11. -libjars hadoop-0.20.2-test.jar \  
  12.    
  13. -input file:///home/xxx/pipes_test/data.txt \   
  14.    
  15. -output file:///home/xxx/pipes_output \   
  16.    
  17. -program /user/XXX/bin/wordcount-nopipe  
5. Hadoop pipes高级编程
如果用户需要在mapreduce作业中加载词典或者传递参数,可这样做:
(1) 提交作业时,用-files选项,将词典(需要传递参数可以放到一个配置文件中)上传给各个datanode,如:
  1. bin/hadoop pipes \  
  2.    
  3. -D hadoop.pipes.java.recordreader=false \  
  4.    
  5. -D hadoop.pipes.java.recordwriter=false \  
  6.    
  7. -D mapred.job.name=wordcount \  
  8.    
  9. -files dic.txt \  
  10.    
  11. ….  
(2) 在Mapper或者Reducer的构造函数中,将字典文件以本地文件的形式打开,并把内容保存到一个map或者set中,然后再map()或者reduce()函数中使用即可,如:
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作业变得可能,它简单好用,提供了用户所需的大部分功能。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款