Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created October 5, 2012 13:08
Show Gist options
  • Save geofferyzh/3839714 to your computer and use it in GitHub Desktop.
Save geofferyzh/3839714 to your computer and use it in GitHub Desktop.
Hadoop 101 - Descending Sort
// Set Sort Comparator Class in the Driver code
job.setSortComparatorClass(SortFloatComparator.class);
// Write a Sort Comparator and save as "SortFloatComparator.java"
import java.io.*;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
public class SortFloatComparator extends WritableComparator {
//Constructor.
protected SortFloatComparator() {
super(FloatWritable.class, true);
}
@SuppressWarnings("rawtypes")
@Override
public int compare(WritableComparable w1, WritableComparable w2) {
FloatWritable k1 = (FloatWritable)w1;
FloatWritable k2 = (FloatWritable)w2;
return -1 * k1.compareTo(k2);
}
}
// Mapper and Reducer Codes here...
@Moh2012
Copy link

Moh2012 commented Mar 1, 2018

Hi,
I would like to thank you for posting this here, that is very helpful.
I am actually trying to let it run on my word count program, but I am stuck there.
Could u please post the whole code.

Thank You

@gabrielnan
Copy link

Thanks! Very useful for me as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment