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

【Hadoop学习】在伪分布式Hadoop上实践word count程序——C/C++ Pipes版本

[日期:2012-10-24] 来源:Linux社区  作者:xceman1997 [字体:大 中 小]

就像上一篇文章(http://www.linuxidc.com/Linux/2012-10/72857.htm )中说的,其实最大的障碍在于Hadoop自带的pipes静态库和动态库都是为linux平台的,而不是为MacOS平台的。在MacOS下,想要使用pipes,需要重新编译库文件。编译过程和方法见上一篇博文。

其他的,似乎没有太多好说的。我就列出代码吧。

hadoopWordCountPipe.cpp的内容如下:

  1. // the header files of haddop  
  2. #include "hadoop/Pipes.hh"  
  3. #include "hadoop/TemplateFactory.hh"  
  4. #include "hadoop/StringUtils.hh"  
  5.  
  6. #include <string>  
  7. #include <vector>  
  8.  
  9. using namespace std; 
  10.  
  11. const string WORDCOUNT = "WORDCOUNT"; 
  12. const string INPUT_WORDS = "INPUT_WORDS"; 
  13. const string OUTPUT_WORDS = "OUTPUT_WORDS"; 
  14.  
  15. class WordCountMap: public HadoopPipes::Mapper 
  16. { 
  17. public: 
  18.     HadoopPipes::TaskContext::Counter * inputWords; 
  19.  
  20.     WordCountMap (HadoopPipes::TaskContext & context) 
  21.     { 
  22.         inputWords = context.getCounter(WORDCOUNT, INPUT_WORDS); 
  23.     } 
  24.  
  25.     void map (HadoopPipes::MapContext & context) 
  26.     { 
  27.         vector<string> WordVec = HadoopUtils::splitString (context.getInputValue(), " "); 
  28.         for (int i=0; i<(int)WordVec.size(); i++) 
  29.             context.emit (WordVec.at(i), "1"); 
  30.         context.incrementCounter (inputWords, WordVec.size()); 
  31.     } 
  32. }; 
  33.  
  34. class WordCountReduce: public HadoopPipes::Reducer 
  35. { 
  36. public: 
  37.     HadoopPipes::TaskContext::Counter * outputWords; 
  38.  
  39.     WordCountReduce (HadoopPipes::TaskContext & context) 
  40.     { 
  41.         outputWords = context.getCounter (WORDCOUNT, OUTPUT_WORDS); 
  42.     } 
  43.  
  44.     void reduce (HadoopPipes::ReduceContext & context) 
  45.     { 
  46.         int sum = 0; 
  47.         while (context.nextValue()) 
  48.         { 
  49.             sum += HadoopUtils::toInt (context.getInputValue()); 
  50.         } 
  51.         context.emit (context.getInputKey(), HadoopUtils::toString(sum)); 
  52.         context.incrementCounter (outputWords, 1); 
  53.     } 
  54. }; 
  55.  
  56. int main (int argc, char * argv[]) 
  57. { 
  58.     return HadoopPipes::runTask (HadoopPipes::TemplateFactory<WordCountMap, WordCountReduce>()); 
  59. } 

Makefile的内容如下:

  1. HADOOP_INSTALL="/Volumes/Data/Works/Hadoop/hadoop-0.20.2" 
  2. PLATFORM=Mac_OS_X-x86_64-64 
  3.  
  4. CC = g++ 
  5. CPPFLAGS = -m64 -I$(HADOOP_INSTALL)/c++/$(PLATFORM)/include   
  6.  
  7. hadoopWordCountPipe: hadoopWordCountPipe.cpp 
  8.     $(CC) $(CPPFLAGS) $< -Wall -L$(HADOOP_INSTALL)/c++/$(PLATFORM)/lib -lhadooppipes -lhadooputils -lpthread -g -O2 -o $@ 
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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