mapPartitionsWithIndex

This is similar to mapPartitions. However, in this transformation, the value of the partition index is also available. We showed an example of this transformation in the first section of this chapter (RDD partitioning) while discussing partitioners.

In the following example, a mapPartitionswithIndex transformation is performed over int RDD to find out which elements belong to which partition:

Java 7:

intRDD.mapPartitionsWithIndex(new Function2<Integer, Iterator<Integer>, Iterator<String>>() {
@Override
public Iterator<String> call(Integer index, Iterator<Integer> iterator) throws Exception {
List<String> list =new ArrayList<String>();
while(iterator.hasNext())
{
list.add("Element "+iterator.next()+" belongs to partition "+index);
}
return list.iterator();
}
}, false);

Java 8:

intRDD.mapPartitionsWithIndex((index, iterator) -> {
List<String> list = new ArrayList<String>();
while (iterator.hasNext()) {
list.add("Element " + iterator.next() + " belongs to partition " + index);
}
return list.iterator();
}, false);

It also accepts a Boolean parameter which signifies the value of preservesPartitioning.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.88.110