Assuming you are using Hadoop Streaming, you need to use the KeyFieldBasedComparator class.

  1. -D mapred.output.key.comparator.class=org.apache.hadoop.mapred.lib.KeyFieldBasedComparator should be added to streaming command
  2. You need to provide type of sorting required using mapred.text.key.comparator.options. Some useful ones are -n : numeric sort, -r : reverse sort


Create an identity mapper and reducer with the following code

This is the mapper.py & reducer.py


This is the input.txt

This is the Streaming command

And you will get the required output


  1. I have used a simple one key input. If however you have multiple keys and/or partitions, you will have to edit mapred.text.key.comparator.options as needed. Since I do not know your use case , my example is limited to this
  2. Identity mapper is needed since you will need atleast one mapper for a MR job to run.
  3. Identity reducer is needed since shuffle/sort phase will not work if it is a pure map only job.


Leave a Reply

Your email address will not be published.