The first program of Hadoop Big Data--wordcount

Source: Internet
Author: User
Tags map class

Map class:

1  PackageLYC.YUSHAO.HADOOP.MR.WC;2 3 Importjava.io.IOException;4 5 Importorg.apache.hadoop.io.LongWritable;6 ImportOrg.apache.hadoop.io.Text;7 ImportOrg.apache.hadoop.mapreduce.Mapper;8 9  Public classWcmapperextendsmapper<longwritable, text, text, longwritable> {Ten  One @Override A     protected voidmap (longwritable key, Text value, context context) -             throwsIOException, interruptedexception { -         //first, receive the data the         //Accept Data -String line =value.tostring (); -         //to split -         //Split +string[] Words = Line.split (""); -         //to cycle +         //Loop A          for(String w:words) { at             //Send -             //Send -Context.write (NewText (W),NewLongwritable (1)); -         } -  -     } in  -}

Reduce class:

1  PackageLYC.YUSHAO.HADOOP.MR.WC;2 3 Importjava.io.IOException;4 5 Importorg.apache.hadoop.io.LongWritable;6 ImportOrg.apache.hadoop.io.Text;7 ImportOrg.apache.hadoop.mapreduce.Reducer;8 9  Public classWcreducerextendsReducer<text, Longwritable, Text, longwritable> {Ten  One @Override A     protected voidReduce (Text key, iterable<longwritable>values, -Context context)throwsIOException, interruptedexception { -         //Define a counter the         //Define a counter -         LongCounter = 0; -  -         //receiving data Loops +         //Accept data and loop -          for(longwritable i:values) { +             //sum ACounter + =i.get (); at  -         } -         //Send -Context.write (Key,Newlongwritable (counter)); -  -     } in  -}

WordCount class:

1  PackageLYC.YUSHAO.HADOOP.MR.WC;2 3 Importorg.apache.hadoop.conf.Configuration;4 ImportOrg.apache.hadoop.fs.Path;5 Importorg.apache.hadoop.io.LongWritable;6 ImportOrg.apache.hadoop.io.Text;7 ImportOrg.apache.hadoop.mapreduce.Job;8 ImportOrg.apache.hadoop.mapreduce.lib.input.FileInputFormat;9 ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;Ten  One  Public classWordCount { A  -      Public Static voidMain (string[] args)throwsException { -Configuration conf =NewConfiguration (); the         //abstract MapReduce into a single job -Job Job =job.getinstance (conf); -  -         //Notice!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +Job.setjarbyclass (WordCount.class); -  +         //assemble a custom class A  at         //Set Mapper ' s properties -Job.setmapperclass (Wcmapper.class); -  -Job.setmapoutputkeyclass (Text.class); -  -Job.setmapoutputvalueclass (longwritable.class); in  -         //reading HDFs Data toFileinputformat.setinputpaths (Job,NewPath ("/words.txt")); +  -         //Set Reducer ' s properties theJob.setreducerclass (Wcreducer.class); *         //output to HDFs inside $Job.setoutputkeyclass (Text.class);Panax NotoginsengJob.setoutputvalueclass (longwritable.class); -Fileoutputformat.setoutputpath (Job,NewPath ("/wcout111")); the  +         //Some methods of invoking the job to commit A         //submit,but This isn't good the         //job.submit (); +Job.waitforcompletion (true); -     } $  $}

Right-click: project Name

Export

JAR File

Finish

On the command line:

Hadoop Jar/root/mrs.jar

success!!

The first program of Hadoop Big Data--wordcount

Related Article

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.