1. After opening eclipse, build the WordCount project
PackageWordCount;Importjava.io.IOException; ImportJava.util.StringTokenizer; Importorg.apache.hadoop.conf.Configuration; ImportOrg.apache.hadoop.fs.Path; Importorg.apache.hadoop.io.IntWritable; Importorg.apache.hadoop.io.LongWritable; ImportOrg.apache.hadoop.io.Text; ImportOrg.apache.hadoop.mapreduce.Job; ImportOrg.apache.hadoop.mapreduce.Mapper; ImportOrg.apache.hadoop.mapreduce.Reducer; ImportOrg.apache.hadoop.mapreduce.lib.input.FileInputFormat; ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat; Public classWordCount { Public Static classTokenizermapperextendsmapper<longwritable, text, text, intwritable>{ Private Final StaticIntwritable one =NewIntwritable (1); PrivateText Word =NewText (); Public voidmap (longwritable key, Text value, context context)throwsIOException, interruptedexception {stringtokenizer ITR=NewStringTokenizer (value.tostring ()); while(Itr.hasmoretokens ()) {Word.set (Itr.nexttoken ()); Context.write (Word, one); } } } Public Static classIntsumreducerextendsReducer<text, Intwritable, Text, intwritable> { Privateintwritable result =Newintwritable (); Public voidReduce (Text key, iterable<intwritable>values, context context)throwsIOException, interruptedexception {intsum = 0; for(intwritable val:values) {sum+=Val.get (); } result.set (sum); Context.write (key, result); } } Public Static voidMain (string[] args)throwsException {Configuration conf=NewConfiguration (); if(Args.length! = 2) {System.err.println ("Usage:wordcount"); System.exit (2); } Job Job=NewJob (conf, "word count"); Job.setjarbyclass (WordCount.class); Job.setmapperclass (tokenizermapper.class); Job.setreducerclass (intsumreducer.class); Job.setmapoutputkeyclass (Text.class); Job.setmapoutputvalueclass (intwritable.class); Job.setoutputkeyclass (Text.class); Job.setoutputvalueclass (intwritable.class); Fileinputformat.addinputpath (Job,NewPath (args[0])); Fileoutputformat.setoutputpath (Job,NewPath (args[1])); System.exit (Job.waitforcompletion (true) ? 0:1); } }
2. Configure the Hadoop path.
Put the files that need to be run into the input folder, how to configure the file path and run result path on the run configuration on Eclipse, separated by a space, how to click Apply-run and start running.
3. View results with terminal
jias-macbook-pro:output jia$ cat part-r-00000 do 2excuse 1fine 1hello 2how 1me 1thank 2you 3