pyt*_*nic 7 scala bigdata apache-spark
spark中的flatMap是否像map函数一样,因此不会导致混乱,或者是否会触发shuffle.我怀疑它确实导致了改组.有人可以证实吗?
小智 7
map或flatMap都没有改组.导致洗牌的操作是:
尽管新洗牌数据的每个分区中的元素集将是确定性的,并且分区本身的排序也是如此,但这些元素的排序不是.如果在随机播放后需要可预测的有序数据,则可以使用:
更多信息:http://spark.apache.org/docs/latest/programming-guide.html#shuffle-operations
没有洗牌。以下是这两个函数的来源:
/**
* Return a new RDD by applying a function to all elements of this RDD.
*/
def map[U: ClassTag](f: T => U): RDD[U] = withScope {
val cleanF = sc.clean(f)
new MapPartitionsRDD[U, T](this, (context, pid, iter) => iter.map(cleanF))
}
/**
* Return a new RDD by first applying a function to all elements of this
* RDD, and then flattening the results.
*/
def flatMap[U: ClassTag](f: T => TraversableOnce[U]): RDD[U] = withScope {
val cleanF = sc.clean(f)
new MapPartitionsRDD[U, T](this, (context, pid, iter) => iter.flatMap(cleanF))
}
Run Code Online (Sandbox Code Playgroud)
如您所见,RDD.flatMap只需调用flatMap代表分区的 Scala 迭代器即可。