1 PackageKeysort;2 Importjava.io.IOException;3 4 Importorg.apache.hadoop.conf.Configuration;5 ImportOrg.apache.hadoop.io.Text;6 ImportOrg.apache.hadoop.mapreduce.Job;7 ImportOrg.apache.hadoop.mapreduce.Mapper;8 ImportOrg.apache.hadoop.mapreduce.Partitioner;9 ImportOrg.apache.hadoop.mapreduce.lib.input.FileInputFormat;Ten ImportOrg.apache.hadoop.mapreduce.lib.output.FileOutputFormat; One ImportOrg.apache.hadoop.fs.Path; A Public classKeysort { - Public Static classMymapperextendsMapper<object, text, text, text> { - PrivateText map_value=NewText ("Hello"); the Public voidMap (Object key,text value,context Context)throwsIOException, interruptedexception{ - context.write (value, map_value); - } - } + - Public Static classMypartitionerextendsPartitioner<text, text>{ + PublicText realkey=NewText (); A @Override at Public intGetpartition (text key, text value,intnumredtasks) { - //TODO Auto-generated method stubs -String[] Kk=key.tostring (). Split ("\ T"); -Realkey.set (kk[0]); -SYSTEM.OUT.PRINTLN ("Partition key is:" +realkey.tostring ()); - return((Realkey.hashcode () &integer.max_value)%numredtasks); in } - to } + - Public Static voidMain (string[] args)throwsException the { * if(args.length!=2){ $System.err.println ("usage:matrixmultiply <inputPathM> <inputPathN> <outputPath>");Panax NotoginsengSystem.exit (2); - } theConfiguration conf=NewConfiguration (); +Job job=NewJob (conf, "Seconderysort"); AJob.setnumreducetasks (1);//when set to 0, the partitioning process is not performed and cannot be exploited with Jian Zhi theJob.setjarbyclass (Keysort.class); +Job.setmapperclass (Mymapper.class); -Job.setpartitionerclass (Mypartitioner.class); $Job.setoutputkeyclass (Text.class); $Job.setoutputvalueclass (Text.class); -Fileinputformat.setinputpaths (Job,NewPath (args[0])); -Fileoutputformat.setoutputpath (Job,NewPath (args[1])); theSystem.exit (Job.waitforcompletion (true)? 0:1); - Wuyi } the - } Wu - About $ -
To sort key and value using composite key-value pairs