spark AE功能控制小文件的输出数量

本文介绍Spark自适应执行机制如何动态调整shuffle读取的分区数,优化reduce任务数量,提高小任务的运行效率并减少输出文件数目。文章讨论了相关配置参数的作用及源码实现细节。

场景:

一般我们配置spark.sql.shuffle.partition来控制reduce的个数,但是对于不同的作业,reduce个数是不同的,对于小任务,一方面是影响运行效率,另一方面对于产生的文件数有很大的影响。

 

描述:

spark自适应执行,可以动态控制shuffle read的分区数,对reduce个数进行动态调整。

 

命令:

spark.sql.adaptive.enabled设置自适应执行
spark.sql.adaptive.minNumPostShufflePartitionsreduce阶段最小的分区数
spark.sql.adaptive.shuffle.targetPostShuffleInputSize reduce阶段每个task最少处理的数据量,默认64M
spark.sql.adaptive.shuffle.targetPostShuffleRowCountreduce每个分区处理的行数(这个在spark-2.3已经没有了)

 

 

 

 

 

源码分析:

以spark-2.3为例, ExchangeCoordinator类。如果设置了最小分区数(spark.sql.adaptive.minNumPostShufflePartitions), 就用最小分区数来计算每个分区inputsize的大小。如果没有设置最小分区数,直接使用spark.sql.adaptive.shuffle.targetPostShuffleInputSize来作为每个分区inputsize的大小。
def estimatePartitionStartIndices(
      mapOutputStatistics: Array[MapOutputStatistics]): Array[Int] = {
    // If we have mapOutputStatistics.length < numExchange, it is because we do not submit
    // a stage when the number of partitions of this dependency is 0.
    assert(mapOutputStatistics.length <= numExchanges)

    // If minNumPostShufflePartitions is defined, it is possible that we need to use a
    // value less than advisoryTargetPostShuffleInputSize as the target input size of
    // a post shuffle task.
    //如果最小分区数已经设置,每个分区的inputsize的大小= min(总数据量大小/ 最小分区数, 设置的单个分区的inputsize的大小)
    val targetPostShuffleInputSize = minNumPostShufflePartitions match {
      case Some(numPartitions) =>
        val totalPostShuffleInputSize = mapOutputStatistics.map(_.bytesByPartitionId.sum).sum
        // The max at here is to make sure that when we have an empty table, we
        // only have a single post-shuffle partition.
        // There is no particular reason that we pick 16. We just need a number to
        // prevent maxPostShuffleInputSize from being set to 0.
        val maxPostShuffleInputSize =
          math.max(math.ceil(totalPostShuffleInputSize / numPartitions.toDouble).toLong, 16)
        math.min(maxPostShuffleInputSize, advisoryTargetPostShuffleInputSize)

      case None => advisoryTargetPostShuffleInputSize
    }

    logInfo(
      s"advisoryTargetPostShuffleInputSize: $advisoryTargetPostShuffleInputSize, " +
      s"targetPostShuffleInputSize $targetPostShuffleInputSize.")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值