Hadoop Learning-wordcount Program C + + rewrite execution

Source: Internet
Author: User

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&apos;s hand. It?? s about have each tiny wish come true, or have something to eat if you are hungry or have someone&apos;s love When your need love
Happiness is isn't about being immortal nor has food or rights in One&apos;s hand. It?? s about have each tiny wish come true, or have something to eat if you are hungry or have someone&apos;s love When your need love
Happiness is isn't about being immortal nor has food or rights in One&apos;s hand. It?? s about have each tiny wish come true, or have something to eat if you are hungry or have someone&apos;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&apos;s 3
Or 9
Rights 3
Someone&apos;s 3
Something 3
Tiny 3
To 3
True 3
When 6
Wish 3
You 6



Hadoop Learning-wordcount Program C + + rewrite execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.