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

2019-03-28 13:21|来源: 网络

就像上一篇文章(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. public
  17.     HadoopPipes::TaskContext::Counter * inputWords; 
  18.  
  19.     WordCountMap (HadoopPipes::TaskContext & context) 
  20.     { 
  21.         inputWords = context.getCounter(WORDCOUNT, INPUT_WORDS); 
  22.     } 
  23.  
  24.     void map (HadoopPipes::MapContext & context) 
  25.     { 
  26.         vector<string> WordVec = HadoopUtils::splitString (context.getInputValue(), " "); 
  27.         for (int i=0; i<(int)WordVec.size(); i++) 
  28.             context.emit (WordVec.at(i), "1"); 
  29.         context.incrementCounter (inputWords, WordVec.size()); 
  30.     } 
  31. }; 
  32.  
  33. class WordCountReduce: public HadoopPipes::Reducer 
  34. public
  35.     HadoopPipes::TaskContext::Counter * outputWords; 
  36.  
  37.     WordCountReduce (HadoopPipes::TaskContext & context) 
  38.     { 
  39.         outputWords = context.getCounter (WORDCOUNT, OUTPUT_WORDS); 
  40.     } 
  41.  
  42.     void reduce (HadoopPipes::ReduceContext & context) 
  43.     { 
  44.         int sum = 0; 
  45.         while (context.nextValue()) 
  46.         { 
  47.             sum += HadoopUtils::toInt (context.getInputValue()); 
  48.         } 
  49.         context.emit (context.getInputKey(), HadoopUtils::toString(sum)); 
  50.         context.incrementCounter (outputWords, 1); 
  51.     } 
  52. }; 
  53.  
  54. int main (int argc, char * argv[]) 
  55.     return HadoopPipes::runTask (HadoopPipes::TemplateFactory<WordCountMap, WordCountReduce>()); 

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 $@ 

相关问答

更多