1. Program execution Command:
Hadoop pipes-d hadoop.pipes.java.recordreader=true-d hadoop.pipes.java.recordwriter=true-input/input/wordcount/ Sample.txt-output/output/wordcount-program/bin/wordcount
2, the specific code:
#include <algorithm> #include <stdint.h> #include <string> #include <vector> #include "pipes.hh " #include" templatefactory.hh " #include" stringutils.hh "#include <iostream>using namespace Std;class Wordcountmapper:public hadooppipes::mapper {public : Wordcountmapper (hadooppipes::taskcontext& Context);vector<string> split (const string& SRC, const string& separator); void map (hadooppipes::mapcontext& context);}; Class Wordcountreducer:public Hadooppipes::reducer {public : wordcountreducer (Hadooppipes::taskcontext & context); void reduce (hadooppipes::reducecontext& context);};
#include "wordcount.h" Wordcountmapper::wordcountmapper (hadooppipes::taskcontext& context) {}void Wordcountmapper::map (hadooppipes::mapcontext& context) {int count = 1; string line = Context.getinputvalue (); Vector<string> Wordvec = Split (line, ""); for (unsigned i=0; I<wordvec.size (); i++) {context.emit (Wordvec[i], hadooputils::tostring (count));}} Vector<string> wordcountmapper::split (const string& SRC, const string& separator) {vector<string> Dest string str = src; string substring; String::size_type start = 0, index = 0;while (Index! = string::npos) {index = str.find_first_of (Separator,start) ; if (Index! = string::npos) {substring = STR.SUBSTR (Start,index-start); Dest.push_back (substring); Start = Str.find_first_not_of (Separator,index); if (start = = String::npos) return dest; }} substring = Str.substr (start); Dest.push_back (substring); Return dest;} Wordcountreducer::wordcountreducer (hadooppipes::taskcontext& context) {}void Wordcountreducer::reduce ( hadooppipes::reducecontext& context) {int wsum = 0; while (Context.nextvalue ()) {wsum = Wsum + hadooputils::toint (Context.getinputvalue ()); } context.emit (Context.getinputkey (), hadooputils::tostring (wsum)); }
#include "wordcount.h" int main (int argc, char *argv[]) { return Hadooppipes::runtask (hadooppipes::templatefactory <wordcountmapper, wordcountreducer> ()); }
Makefile Program:
. Suffixes:.h. C. cpp. occ=g++cppflags =-m64 RM = Rmsrcs = Wordcount.cpp Main.cppprogram = wordcountobjs=$ (SRCS:.CPP=.O) INC _path =-i$ (hadoop_dev_home)/includelib_path =-l$ (hadoop_dev_home)/lib/nativelibs =-lhadooppipes-lcrypto- Lhadooputils-lpthread#$? Indicates the dependency [email protected] represents the destination item $ (program): $ (OBJS) $ (CC) $? -wall $ (Lib_path) $ (LIBS) -g-o2-o [email protected]$ (OBJS): $ (SRCS) $ (CC) $ (cppflags)-C $ (SRCS) $ (inc_path). phony:cleanclean:$ (RM) $ (program) $ (OBJS)
Source data:
Happiness is isn't about being immortal nor has food or rights in One's hand. It?? s about have each tiny wish come true, or have something to eat if you are hungry or have someone's love When your need love
Happiness is isn't about being immortal nor has food or rights in One's hand. It?? s about have each tiny wish come true, or have something to eat if you are hungry or have someone's love When your need love
Happiness is isn't about being immortal nor has food or rights in One's hand. It?? s about have each tiny wish come true, or have something to eat if you are hungry or have someone's love When your need love
Execution Result:
Happiness 3
It?? S 3
About 6
Is 3
Being 3
Come 3
each 3
Eat 3
Food 3
Hand. 3
Having 12
Hungry 3
Immortal 3
Inch 3
Is 3
Love 6
Need 3
Nor 3
Not 3
One's 3
Or 9
Rights 3
Someone's 3
Something 3
Tiny 3
To 3
True 3
When 6
Wish 3
You 6
Hadoop Learning-wordcount Program C + + rewrite execution