MapReduce Sort Output

Source: Internet
Author: User

The Hadoop map is a continuous learning ~ with output auto-sorting function ~

Importorg.apache.hadoop.conf.Configuration;Importorg.apache.hadoop.conf.Configured;ImportOrg.apache.hadoop.fs.Path;Importorg.apache.hadoop.io.IntWritable;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.input.TextInputFormat;ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;ImportOrg.apache.hadoop.util.Tool;ImportOrg.apache.hadoop.util.ToolRunner;Importjava.io.IOException; Public classSortextendsConfiguredImplementsTool {
Here the map converts the input value to the Intwritable type as the output key Public Static classMapextendsMapper<object,text,intwritable,intwritable> { Private Staticintwritable data =Newintwritable (); Public voidMap (Object key,text value,context Context)throwsioexception,interruptedexception {String line=value.tostring (); System.out.println ("Line" +Line ); Data.set (Integer.parseint (line)); Context.write (data,NewIntwritable (1)); }}//reduce copies the input key to the output value, and then determines the number of key outputs based on the number of elements in the input value-list Public Static classReduceextendsReducer<intwritable,intwritable,intwritable,intwritable> {
Global linenum to represent Key's precedencePrivate StaticIntwritable LineNum =NewIntwritable (1); Public voidReduce (intwritable key,iterable<intwritable> values,context Context)throwsioexception,interruptedexception{ for(intwritable val:values) {context.write (Linenum,key); System.out.println (LineNum+" "+key); LineNum=NewIntwritable (Linenum.get () +1); } } } Public intRun (string[] args)throwsexception{Configuration AAA=NewConfiguration (); Job Job=job.getinstance (AAA); String inputpaths= "/usr/local/idea-ic-139.1117.1/hadoop/out/datainput/sort.txt"; String OutputPath= "/usr/local/idea-ic-139.1117.1/hadoop/out/dataout/"; Job.setjarbyclass (Sort.class); Job.setjobname ("Sort"); Job.setoutputkeyclass (Text.class); Job.setoutputvalueclass (intwritable.class); Job.setmapperclass (Map.class); Job.setreducerclass (Reduce.class); Fileinputformat.setinputpaths (Job,NewPath (inputpaths)); Fileoutputformat.setoutputpath (Job,NewPath (OutputPath)); Job.setoutputkeyclass (intwritable.class); Job.setoutputvalueclass (intwritable.class); BooleanSuccess = Job.waitforcompletion (true); returnSuccess? 0:1; } Public Static voidMain (string[] args)throwsexception{intret = Toolrunner.run (NewSort (), args); System.exit (ret); }}

MapReduce Sort Output

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.