gpt4 book ai didi

apache-spark - mapPartitions 返回空数组

转载 作者:行者123 更新时间:2023-12-04 05:19:01 25 4
gpt4 key购买 nike

我有以下 RDD,它有 4 个分区:-

val rdd=sc.parallelize(1 to 20,4)

现在我尝试在此调用 mapPartitions:-
scala> rdd.mapPartitions(x=> { println(x.size); x }).collect
5
5
5
5
res98: Array[Int] = Array()

为什么它返回空数组? anonymoys 函数只是返回它接收到的相同迭代器,那么它是如何返回空数组的呢?有趣的是,如果我删除 println 语句,它确实返回非空数组:-
scala> rdd.mapPartitions(x=> { x }).collect
res101: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

这个我不明白。为什么 println (它只是打印迭代器的大小)的存在会影响函数的最终结果?

最佳答案

那是因为 xTraversableOnce ,这意味着您通过调用 size 遍历了它然后把它还回来....空的。

您可以通过多种方式解决此问题,但这里有一个:

rdd.mapPartitions(x=> {
val list = x.toList;
println(list.size);
list.toIterator
}).collect

关于apache-spark - mapPartitions 返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32042151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com