就像上一篇文章(http://www.linuxidc.com/Linux/2012-10/72857.htm )中说的,其实最大的障碍在于Hadoop自带的pipes静态库和动态库都是为linux平台的,而不是为MacOS平台的。在MacOS下,想要使用pipes,需要重新编译库文件。编译过程和方法见上一篇博文。
其他的,似乎没有太多好说的。我就列出代码吧。
hadoopWordCountPipe.cpp的内容如下:
- // the header files of haddop
- #include "hadoop/Pipes.hh"
- #include "hadoop/TemplateFactory.hh"
- #include "hadoop/StringUtils.hh"
- #include <string>
- #include <vector>
- using namespace std;
- const string WORDCOUNT = "WORDCOUNT";
- const string INPUT_WORDS = "INPUT_WORDS";
- const string OUTPUT_WORDS = "OUTPUT_WORDS";
- class WordCountMap: public HadoopPipes::Mapper
- {
- public:
- HadoopPipes::TaskContext::Counter * inputWords;
- WordCountMap (HadoopPipes::TaskContext & context)
- {
- inputWords = context.getCounter(WORDCOUNT, INPUT_WORDS);
- }
- void map (HadoopPipes::MapContext & context)
- {
- vector<string> WordVec = HadoopUtils::splitString (context.getInputValue(), " ");
- for (int i=0; i<(int)WordVec.size(); i++)
- context.emit (WordVec.at(i), "1");
- context.incrementCounter (inputWords, WordVec.size());
- }
- };
- class WordCountReduce: public HadoopPipes::Reducer
- {
- public:
- HadoopPipes::TaskContext::Counter * outputWords;
- WordCountReduce (HadoopPipes::TaskContext & context)
- {
- outputWords = context.getCounter (WORDCOUNT, OUTPUT_WORDS);
- }
- void reduce (HadoopPipes::ReduceContext & context)
- {
- int sum = 0;
- while (context.nextValue())
- {
- sum += HadoopUtils::toInt (context.getInputValue());
- }
- context.emit (context.getInputKey(), HadoopUtils::toString(sum));
- context.incrementCounter (outputWords, 1);
- }
- };
- int main (int argc, char * argv[])
- {
- return HadoopPipes::runTask (HadoopPipes::TemplateFactory<WordCountMap, WordCountReduce>());
- }
Makefile的内容如下:
- HADOOP_INSTALL="/Volumes/Data/Works/Hadoop/hadoop-0.20.2"
- PLATFORM=Mac_OS_X-x86_64-64
- CC = g++
- CPPFLAGS = -m64 -I$(HADOOP_INSTALL)/c++/$(PLATFORM)/include
- hadoopWordCountPipe: hadoopWordCountPipe.cpp
- $(CC) $(CPPFLAGS) $< -Wall -L$(HADOOP_INSTALL)/c++/$(PLATFORM)/lib -lhadooppipes -lhadooputils -lpthread -g -O2 -o $@
