为什么硬编码重新分区值

Why hardcode repartition value

查看一些示例 spark 代码,我发现重新分区或合并中的数字是硬编码的:

val resDF = df.coalesce(16)

什么是管理此参数的最佳方法,当集群可以在几秒钟内动态 updated 时,此硬编码值变得无关紧要。

好吧,在示例中看到硬编码值很常见,所以您不必担心,我的意思是您可以随意修改示例。我的意思是分区文档充满了硬编码值,但这些值只是示例。

关于分区数量的经验法则是:

one would want his RDD to have as many partitions as the product of the number of executors by the number of used cores by 3 (or maybe 4). Of course, that's a heuristic and it really depends on your application, dataset and cluster configuration.

但是,请注意重新分区不是免费的,因此在动态环境非常剧烈的情况下,您必须确保重新分区的开销对于您通过此操作获得的收益可以忽略不计。

正如我在 answer 中提到的那样,合并和重新分区可能具有不同的成本。