Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,12 @@ private[spark] class MapPartitionsRDD[U: ClassTag, T: ClassTag](
prev = null
}
}

private[spark] final class PreserveLocationsRDD[U: ClassTag, T: ClassTag](
prev: RDD[T],
f: (TaskContext, Int, Iterator[T]) => Iterator[U], // (TaskContext, partition index, iterator)
preservesPartitioning: Boolean = false, p: (Int) => Seq[String])
extends MapPartitionsRDD[U, T](prev, f, preservesPartitioning) {

override def getPreferredLocations(split: Partition): Seq[String] = p(split.index)
}
11 changes: 11 additions & 0 deletions core/src/main/scala/org/apache/spark/rdd/RDD.scala
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,17 @@ abstract class RDD[T: ClassTag](
preservesPartitioning)
}

def mapPartitionsWithIndexPreserveLocations[U: ClassTag](
f: (Int, Iterator[T]) => Iterator[U],
p: (Int) => Seq[String],
preservesPartitioning: Boolean = false): RDD[U] = withScope {
val cleanedF = sc.clean(f)
new PreserveLocationsRDD(
this,
(context: TaskContext, index: Int, iter: Iterator[T]) => cleanedF(index, iter),
preservesPartitioning, p)
}

/**
* Zips this RDD with another one, returning key-value pairs with the first element in each RDD,
* second element in each RDD, etc. Assumes that the two RDDs have the *same number of
Expand Down