The RDD action top returns a list of n elements based on natural or custom ordering of the elements in the RDD:
//top()
JavaRDD<Integer> intRDD = sparkContext.parallelize(Arrays.asList(1,4,3));
List<Integer> topTwo=intRDD.top(2);
for(Integer intVal:topTwo) {
System.out.println("The top two values of the RDD are : "+intVal);
}
For custom ordering of the elements a comparator is passed as an argument to the top() method:
Java 7:
//top()
JavaRDD<Integer> intRDD = sparkContext.parallelize(Arrays.asList(1,4,3));
List<Integer> topTwo=intRDD.top(2,new integerReverseComparator());
for(Integer intVal:topTwo) {
System.out.println("The top two values of the RDD are : "+intVal);
}
//Also create a Comparator class as below
static class integerReverseComparator implements
Comparator<Integer>, Serializable {
private static finallongserialVersionUID = 1L;
@Override
public int compare(Integer a, Integer b) {
return b.compareTo(a);
}
}
Java 8:
//top()
JavaRDD<Integer> intRDD = sparkContext.parallelize(Arrays.asList(1,4,3));
List<Integer> topTwo=intRDD.top(2,Comparator.reverseOrder());
for(Integer intVal:topTwo) {
System.out.println("The top two values of the RDD are : "+intVal);
}