下面我有一个 Spark 的 Scala 示例 fold
行动:
val rdd1 = sc.parallelize(List(1,2,3,4,5), 3)
rdd1.fold(5)(_ + _)
这会产生输出
35
.有人可以详细解释这个输出是如何计算的吗?
最佳答案
取自 Scaladocs here (强调我的):
@param zeroValue the initial value for the accumulated result of each partition for the
op
operator, and also the initial value for the combine results from different partitions for theop
operator - this will typically be the neutral element (e.g.Nil
for list concatenation or0
for summation)
zeroValue
在您的情况下添加了四次(每个分区一个,在组合分区结果时加一个)。所以结果是:(5 + 1) + (5 + 2 + 3) + (5 + 4 + 5) + 5 // (extra one for combining results)
关于scala - 折叠 Action 在 Spark 中是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48358428/