2nd attempt here, as presented the wrong example initially. From the docs:
preservesPartitioning indicates whether the input function preserves the partitioner, which should be false unless this is a pair RDD and the input function doesn't modify the keys.
Nice prose, but what does it really mean though?
Here a contrived trivial example, and whether or not I pass true or false to mapPartitions, the partitioning of data per partition remains the same for the new RDD even though I alter the K of K,V. So what is the point? Must be something elementary that I am missing.
import org.apache.spark.HashPartitioner
// Some contrived function
def myfunc(iter: Iterator[(String, (Int, String))]) : Iterator[(String, (Int,String))] = {
iter.map{case(x,y) => ("B"+x+"A", y)}
}
val rdd1 = sc.parallelize(1 to 9).map(x => ("RFD"+x+"ABC", (1000, "xc888x"+x))).partitionBy(new HashPartitioner(459))
val rdd2 = rdd1.mapPartitions(myfunc,true) // or false
rdd2.collect
Output of rdd2 for both true and false with mapPartitionsWithIndex reveals in both cases:
res21: Array[String] = Array((BRFD5ABCA,(1000,xc888x5)) -> 22, (BRFD4ABCA,(1000,xc888x4)) -> 66, (BRFD3ABCA,(1000,xc888x3)) -> 110, (BRFD2ABCA,(1000,xc888x2)) -> 154, (BRFD1ABCA,(1000,xc888x1)) -> 198, (BRFD9ABCA,(1000,xc888x9)) -> 305, (BRFD8ABCA,(1000,xc888x8)) -> 349, (BRFD7ABCA,(1000,xc888x7)) -> 393, (BRFD6ABCA,(1000,xc888x6)) -> 437)
which is the same partition distribution for rdd1.
So, what is the point of true or false for preservesPartitioning then?