Take the 2-minute tour ×

In hadoop if you want to group and order something and you write java , the result is that group keys will also be sort defaultly by Lexicographical order,all done with one MR job, so that you spare the another order job.

But now I join using Pig , find a quirky thing.

My input (test.txt) is:


My script is:

A=load 'test.txt' as c1:chararray;
B=group A by c1;
dump B;

The output is:


Why it has group key order depend on string length but alphabetical order.that way I need to do another key order job ,two jobs total,because there is no comparator plugin for group.

Any solution for group&order at once? i would much appreciate.

share|improve this question
Actually, Iam now doing it by first padding the string to the same length. I just curious about why they want to adopt this comparator that differ from original default hadoop experience. –  superChing Jul 5 '13 at 1:19
add comment

2 Answers

Actually, in your case, Pig uses NullTupleWritable for the output key. Since your data is Tuple, it will invoke the comparator DefaultTupleRawComparator. In this class, it will compare the length at first, then compare the content. Follow this link to get details in codes.

share|improve this answer
add comment
up vote 1 down vote accepted

I think I find the answer why that, see: this blog. It is an performance optimization trick by pig. Good for only pure group, bad for group into some order, because you can't force Pig to change comparator selection rule.

share|improve this answer
add comment

Your Answer


