Code test environment: hadoop2.4
Application Scenario: Keys are generally sorted on the CER end, but values are not sorted. You can use this technique to sort values.
Application description:
For example, for the following data:
a,5b,7c,2c,9a,3a,1b,10b,3c,1
If a general Mr is used, the output may be as follows:
a1a3a5b3b10b7c1c9c2
From the data, we can see that the keys are sorted, but their values are not. The skills described in this article can be output as follows:
a1a3a5b3b7b10c1c2c9
This data means that keys and values are sorted.
Quadratic sorting principle:
1) customize the key type and place the value in the key;
2) sort the values by the way using the sort feature of keys;
3) There are two problems:
A. Exceptions may occur when data is transmitted to different reducers;
B. Data is grouped differently in cer Cer;
To solve these two problems, you need to use custom partitioner and custom groupcomparator to define the corresponding logic;
Instance:
Driver Class:
Package FZ. secondarysort; import Org. apache. hadoop. conf. configuration; import Org. apache. hadoop. conf. configured; import Org. apache. hadoop. FS. path; import Org. apache. hadoop. io. intwritable; import Org. apache. hadoop. io. nullwritable; import Org. apache. hadoop. io. text; import Org. apache. hadoop. mapreduce. job; import Org. apache. hadoop. mapreduce. lib. input. fileinputformat; import Org. apache. hadoop. mapreduce. lib. input. Textinputformat; import Org. apache. hadoop. mapreduce. lib. output. fileoutputformat; import Org. apache. hadoop. mapreduce. lib. output. textoutputformat; import Org. apache. hadoop. util. tool; import Org. apache. hadoop. util. toolrunner; public class sortdriver extends configured implements tool {/*** @ Param ARGs * @ throws exception */public static void main (string [] ARGs) throws exception {// todo auto-generated me Thod stubtoolrunner. run (new configuration (), new sortdriver (), argS) ;}@ overridepublic int run (string [] ARGs) throws exception {job job1 = job. getinstance (getconf (), "secondary sort"); job1.setjarbyclass (getclass (); If (ARGs. length! = 5) {system. err. println ("Usage: <input> <output> <numstmcers> <usesecondarysort> <usegroupcomparator>"); system. exit (-1);} path out = New Path (ARGs [1]); out. getfilesystem (getconf ()). delete (Out, true); fileinputformat. setinputpaths (job1, New Path (ARGs [0]); fileoutputformat. setoutputpath (job1, out); If ("true ". equals (ARGs [3]) | "false ". equals (ARGs [3]) {If ("true ". equals (ARGs [3]) {// use secondary sorting job1.setmapperclas S (mapper1.class); job1.setreducerclass (reducer1.class); job1.setmapoutputkeyclass (customkey. class); job1.setmapoutputvalueclass (nullwritable. class); job1.setoutputkeyclass (customkey. class); job1.setoutputvalueclass (nullwritable. class); If ("true ". equals (ARGs [4]) {job1.setgroupingcomparatorclass (customgroupcomparator. class);} else if ("false ". equals (ARGs [4]) {// do nothing} else {system. err. println ("wrong gr OUP comparator argument! "); System. exit (-1);} job1.setpartitionerclass (custompartitioner. class);} else {// do not use secondary sorting job1.setmapperclass (mapper2.class); job1.setreducerclass (reducer2.class); job1.setmapoutputkeyclass (text. class); job1.setmapoutputvalueclass (intwritable. class); job1.setoutputkeyclass (text. class); job1.setoutputvalueclass (intwritable. class) ;}} else {system. err. println ("The fourth argument shoshould be 'true' or 'false'"); s Ystem. exit (-1);} job1.setinputformatclass (textinputformat. class); job1.setoutputformatclass (textoutputformat. class); job1.setnumreducetasks (integer. parseint (ARGs [2]); Boolean job1success = job1.waitforcompletion (true); If (! Job1success) {system. Out. println ("The createbloomfilter job failed! "); Return-1;} return 0 ;}}
Mapper1 (quadratic sorting Mapper ):
Package FZ. secondarysort; import Java. io. ioexception; import Org. apache. hadoop. io. longwritable; import Org. apache. hadoop. io. nullwritable; import Org. apache. hadoop. io. text; import Org. apache. hadoop. mapreduce. mapper;/*** secondary sorting mapper * @ author fansy **/public class mapper1 extendsmapper <longwritable, text, customkey, nullwritable> {private string comma = ","; private customkey newkey = new customkey (); Public void map (longwritable key, text value, context cxt) throws ioexception, interruptedexception {string [] values = value. tostring (). split (comma); newkey. setsymbol (Values [0]); newkey. setvalue (integer. parseint (Values [1]); cxt. write (newkey, nullwritable. get ());}}Reducer1 (secondary sort CER)
Package FZ. secondarysort; import Java. io. ioexception; import Org. apache. hadoop. io. nullwritable; import Org. apache. hadoop. mapreduce. reducer; import Org. slf4j. logger; import Org. slf4j. loggerfactory;/*** secondary sorting CER * @ author fansy **/public class reducer1 extendsreducer <customkey, nullwritable, customkey, nullwritable> {private logger log = loggerfactory. getlogger (reducer1.class); Public void setup (context cxt) {log.info ("reducer1 ********************** in setup ()");} public void reduce (customkey key, iterable <nullwritable> values, context cxt) throws ioexception, interruptedexception {log.info ("reducer1 ******* in reduce ()"); for (nullwritable V: values) {log.info ("key:" + key + "--> value:" + V); cxt. write (Key, V);} log.info ("reducer1 ****** in reduce () ********* end ");}}Unordered mapper2
Package FZ. secondarysort; import Java. io. ioexception; import Org. apache. hadoop. io. intwritable; import Org. apache. hadoop. io. longwritable; import Org. apache. hadoop. io. text; import Org. apache. hadoop. mapreduce. mapper;/*** not secondary sorting * @ author fansy **/public class mapper2 extends mapper <longwritable, text, text, intwritable> {private string comma = ","; public void map (longwritable key, text value, context cxt) throws ioexception, interruptedexception {string [] values = value. tostring (). split (comma); text newkey = new text (Values [0]); cxt. write (newkey, new intwritable (integer. parseint (Values [1]);}
Unordered reducer2
Package FZ. secondarysort; import Java. io. ioexception; import Org. apache. hadoop. io. intwritable; import Org. apache. hadoop. io. text; import Org. apache. hadoop. mapreduce. reducer; import Org. slf4j. logger; import Org. slf4j. loggerfactory;/*** no secondary sorting * @ author fansy **/public class reducer2 extends CER <text, intwritable, text, intwritable> {private logger log = loggerfactory. getlogger (reducer2.class); Public void setup (context cxt) {log.info ("reducer2 ********************** in setup ()");} public void reduce (Text key, iterable <intwritable> values, context cxt) throws ioexception, interruptedexception {log.info ("reducer2 ******* in reduce ()"); for (intwritable V: values) {log.info ("key:" + key + "--> value:" + V); cxt. write (Key, V);} log.info ("reducer2 ****** in reduce () ********* end ");}}
Custom key
Package FZ. secondarysort; import Java. io. datainput; import Java. io. dataoutput; import Java. io. ioexception; import Org. apache. hadoop. io. writablecomparable;/*** symbol is the original key * value is the original value * @ author fansy **/public class customkey implements writablecomparable <customkey> {private int value; private string symbol; @ overridepublic void write (dataoutput out) throws ioexception {out. writeint (this. value); out. Writeutf (this. symbol) ;}@ overridepublic void readfields (datainput in) throws ioexception {This. value = in. readint (); this. symbol = in. readutf () ;}@ overridepublic int compareto (customkey O) {int result = This. symbol. compareto (O. symbol); return result! = 0? Result: This. value-o.value;} @ overridepublic string tostring () {return this. symbol + "\ t" + this. value;} public int getvalue () {return value;} public void setvalue (INT value) {This. value = value;} Public String getsymbol () {return symbol;} public void setsymbol (string symbol) {This. symbol = symbol ;}}Custom groupcomparator
Package FZ. secondarysort; import Org. apache. hadoop. io. writablecomparable; import Org. apache. hadoop. io. writablecomparator;/*** compares only the original key * @ author fansy **/public class customgroupcomparator extends writablecomparator {protected customgroupcomparator () {super (customkey. class, true) ;}@ suppresswarnings ("rawtypes") @ overridepublic int compare (writablecomparable A, writablecomparable B) {customkey aK = (customkey) A; customkey BK = (customkey) b; return AK. getsymbol (). compareto (BK. getsymbol ());}}
Custom partitioner
Package FZ. secondarysort; import Org. apache. hadoop. io. text; import Org. apache. hadoop. mapreduce. partitioner;/*** keep original grouping condition * @ author fansy ** @ Param <k1> * @ Param <V1> */public class custompartitioner <K1, v1> extends partitioner <K1, V1 >{@ overridepublic int getpartition (K1 key, V1 value, int numpartitions) {customkey keyk = (customkey) Key; text tmpvalue = new text (keyk. getsymbol (); Return (tmpvalue. hashcode () & integer. max_value) % numpartitions ;}}
View results:
Do not use secondary sorting
Use secondary sorting and custom group classifier:
We can see that there are only three groups at the CER end, regardless of the quadratic sorting and non-quadratic sorting. The values of the quadratic sorting are also sorted;
If you do not use a group Classifier in secondary sorting, the following result is displayed:
From this result, we can see that there are more than three groups. This result may be problematic (for groups whose key is a, data cannot be integrated );
At the same time, the custom partitioner is used. The difference here is that there is only one reducer. If there are multiple, you can see the difference.
Conclusion: Using secondary sorting, you can sort values not only by key, but also by CER Cer. That is, the values received in each group are sorted, in this way, efficiency can be increased in some operations.
Share, grow, and be happy
Reprinted please indicate blog address: http://blog.csdn.net/fansy1990