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

Hadoop下的Pipes(用C++进行Hadoop程序开发)

[日期:2012-07-21] 来源:Linux社区  作者:lxxgreat [字体:大 中 小]

3.格式化HDFS文件系统

1)命令: Hadoop namenode -format

4.启动守护进程(dfs和mapred,需要正确设置好前面三个文件,否则易出错)

$ start-dfs.sh

$ start-mapred.sh

5.编写CPP文件,源码如下:

  1. #include <algorithm>  
  2. #include <limits>  
  3. #include <stdint.h>  
  4. #include <string>  
  5.   
  6. #include "hadoop/Pipes.hh"  
  7. #include "hadoop/TemplateFactory.hh"  
  8. #include "hadoop/StringUtils.hh"  
  9.   
  10. class MaxTemperatureMapper : public HadoopPipes::Mapper {  
  11. public:  
  12.   MaxTemperatureMapper(HadoopPipes::TaskContext& context) {  
  13.   }  
  14.   void map(HadoopPipes::MapContext& context) {  
  15.     std::string line = context.getInputValue();  
  16.     std::string year = line.substr(15, 4);  
  17.     std::string airTemperature = line.substr(87, 5);  
  18.     std::string q = line.substr(92, 1);  
  19.     if (airTemperature != "+9999" &&  
  20.         (q == "0" || q == "1" || q == "4" || q == "5" || q == "9")) {  
  21.       context.emit(year, airTemperature);  
  22.     }  
  23.   }  
  24. };  
  25.   
  26. class MapTemperatureReducer : public HadoopPipes::Reducer {  
  27. public:  
  28.   MapTemperatureReducer(HadoopPipes::TaskContext& context) {  
  29.   }  
  30.   void reduce(HadoopPipes::ReduceContext& context) {  
  31.     int maxValue = INT_MIN;  
  32.     while (context.nextValue()) {  
  33.       maxValue = std::max(maxValue, HadoopUtils::toInt(context.getInputValue()));  
  34.     }  
  35.     context.emit(context.getInputKey(), HadoopUtils::toString(maxValue));  
  36.   }  
  37. };  
  38.   
  39. int main(int argc, char *argv[]) {  
  40.   return HadoopPipes::runTask(HadoopPipes::TemplateFactory<MaxTemperatureMapper,   
  41.                               MapTemperatureReducer>());  
  42. }  
linux
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/3/31 16:54:58
为什么也运行了一下结果hadoop报错pipe child exception caused by NullException PipesMapperRunner.java:86
请问怎么解决