面试题:Spark中的repartition和coalesce算子区别

579 阅读1分钟

1、repartition

官网定义:

随机地重新排列RDD中的数据以创建更多或更少的分区,并在整个分区之间保持平衡。这一定会在网络上产生大量Shuffle。

底层源码:

def repartition(numPartitions: Int)(implicit ord: Ordering[T] = null): RDD[T] = withScope { coalesce(numPartitions, shuffle = true) }

底层源码终究调用coalesce方法,但是参数sfuffle=true,这说明该算子必定会产生shuffle操作,在生产环境中慎用!!!

2、coalesce

官网定义:

将RDD中的分区数减少到numPartitions。 过滤大型数据集后,对于更有效地运行操作很有用。

使用方法:

coalesce方法与flilter方法合并使用。如果我们传入的参数为true,则会产生shuffle过程,如果传入参数为false,则不会产生shuffle过程