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...
@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