2121import java .io .IOException ;
2222import java .util .ArrayList ;
2323import java .util .Arrays ;
24+ import java .util .Comparator ;
2425
2526import junit .framework .TestCase ;
2627
2728import org .apache .hadoop .conf .Configuration ;
29+ import org .apache .hadoop .fs .CommonConfigurationKeys ;
2830import org .apache .hadoop .fs .FileSystem ;
2931import org .apache .hadoop .fs .Path ;
3032import org .apache .hadoop .io .NullWritable ;
3133import org .apache .hadoop .io .RawComparator ;
3234import org .apache .hadoop .io .SequenceFile ;
3335import org .apache .hadoop .io .Text ;
34- import org .apache .hadoop .io .WritableComparable ;
3536import org .apache .hadoop .io .WritableComparator ;
3637import org .apache .hadoop .io .WritableUtils ;
38+ import org .apache .hadoop .io .SequenceFile .CompressionType ;
39+ import org .apache .hadoop .io .serializer .JavaSerialization ;
40+ import org .apache .hadoop .io .serializer .JavaSerializationComparator ;
41+ import org .apache .hadoop .io .serializer .Serialization ;
42+ import org .apache .hadoop .io .serializer .WritableSerialization ;
3743import org .apache .hadoop .mapreduce .MRJobConfig ;
3844
3945public class TestTotalOrderPartitioner extends TestCase {
@@ -51,6 +57,19 @@ public class TestTotalOrderPartitioner extends TestCase {
5157 new Text ("yak" ), // 9
5258 };
5359
60+ private static final String [] splitJavaStrings = new String [] {
61+ // -inf // 0
62+ new String ("aabbb" ), // 1
63+ new String ("babbb" ), // 2
64+ new String ("daddd" ), // 3
65+ new String ("dddee" ), // 4
66+ new String ("ddhee" ), // 5
67+ new String ("dingo" ), // 6
68+ new String ("hijjj" ), // 7
69+ new String ("n" ), // 8
70+ new String ("yak" ), // 9
71+ };
72+
5473 static class Check <T > {
5574 T data ;
5675 int part ;
@@ -76,19 +95,41 @@ static class Check<T> {
7695 testStrings .add (new Check <Text >(new Text ("hi" ), 6 ));
7796 };
7897
79- private static <T extends WritableComparable <?>> Path writePartitionFile (
98+ private static final ArrayList <Check <String >> testJavaStrings =
99+ new ArrayList <Check <String >>();
100+ static {
101+ testJavaStrings .add (new Check <String >(new String ("aaaaa" ), 0 ));
102+ testJavaStrings .add (new Check <String >(new String ("aaabb" ), 0 ));
103+ testJavaStrings .add (new Check <String >(new String ("aabbb" ), 1 ));
104+ testJavaStrings .add (new Check <String >(new String ("aaaaa" ), 0 ));
105+ testJavaStrings .add (new Check <String >(new String ("babbb" ), 2 ));
106+ testJavaStrings .add (new Check <String >(new String ("baabb" ), 1 ));
107+ testJavaStrings .add (new Check <String >(new String ("yai" ), 8 ));
108+ testJavaStrings .add (new Check <String >(new String ("yak" ), 9 ));
109+ testJavaStrings .add (new Check <String >(new String ("z" ), 9 ));
110+ testJavaStrings .add (new Check <String >(new String ("ddngo" ), 5 ));
111+ testJavaStrings .add (new Check <String >(new String ("hi" ), 6 ));
112+ };
113+
114+
115+ private static <T > Path writePartitionFile (
80116 String testname , Configuration conf , T [] splits ) throws IOException {
81117 final FileSystem fs = FileSystem .getLocal (conf );
82118 final Path testdir = new Path (System .getProperty ("test.build.data" , "/tmp" )
83- ).makeQualified (fs );
119+ ).makeQualified (
120+ fs .getUri (),
121+ fs .getWorkingDirectory ());
84122 Path p = new Path (testdir , testname + "/_partition.lst" );
85123 TotalOrderPartitioner .setPartitionFile (conf , p );
86124 conf .setInt (MRJobConfig .NUM_REDUCES , splits .length + 1 );
87125 SequenceFile .Writer w = null ;
88126 try {
89- w = SequenceFile .createWriter (fs , conf , p ,
90- splits [0 ].getClass (), NullWritable .class ,
91- SequenceFile .CompressionType .NONE );
127+ w = SequenceFile .createWriter (
128+ conf ,
129+ SequenceFile .Writer .file (p ),
130+ SequenceFile .Writer .keyClass (splits [0 ].getClass ()),
131+ SequenceFile .Writer .valueClass (NullWritable .class ),
132+ SequenceFile .Writer .compression (CompressionType .NONE ));
92133 for (int i = 0 ; i < splits .length ; ++i ) {
93134 w .append (splits [i ], NullWritable .get ());
94135 }
@@ -99,6 +140,31 @@ private static <T extends WritableComparable<?>> Path writePartitionFile(
99140 return p ;
100141 }
101142
143+ public void testTotalOrderWithCustomSerialization () throws Exception {
144+ TotalOrderPartitioner <String , NullWritable > partitioner =
145+ new TotalOrderPartitioner <String , NullWritable >();
146+ Configuration conf = new Configuration ();
147+ conf .setStrings (CommonConfigurationKeys .IO_SERIALIZATIONS_KEY ,
148+ JavaSerialization .class .getName (),
149+ WritableSerialization .class .getName ());
150+ conf .setClass (MRJobConfig .KEY_COMPARATOR ,
151+ JavaSerializationComparator .class ,
152+ Comparator .class );
153+ Path p = TestTotalOrderPartitioner .<String >writePartitionFile (
154+ "totalordercustomserialization" , conf , splitJavaStrings );
155+ conf .setClass (MRJobConfig .MAP_OUTPUT_KEY_CLASS , String .class , Object .class );
156+ try {
157+ partitioner .setConf (conf );
158+ NullWritable nw = NullWritable .get ();
159+ for (Check <String > chk : testJavaStrings ) {
160+ assertEquals (chk .data .toString (), chk .part ,
161+ partitioner .getPartition (chk .data , nw , splitJavaStrings .length + 1 ));
162+ }
163+ } finally {
164+ p .getFileSystem (conf ).delete (p , true );
165+ }
166+ }
167+
102168 public void testTotalOrderMemCmp () throws Exception {
103169 TotalOrderPartitioner <Text ,NullWritable > partitioner =
104170 new TotalOrderPartitioner <Text ,NullWritable >();
0 commit comments