Union 原理-源码(spark3.0)

本文深入探讨Spark的Union操作,解释如何处理不同RDD的合并。当RDD具有相同的分区器时,创建PartitionerAwareUnionRDD,否则采用UnionRDD。PartitionerAwareUnionRDD基于oneToOneDependency,而UnionRDD若RDD数量小于10,则进行特定的分区合并,依赖关系为RangeDependency。

       这也是一个action算子,并且不会发生shuffle。首先会将多个上游的RDD 放入到一个集合中,或获取所有RDD的分区,判断 是否都定义了分区 并且分区是一样的,如果是则返回PartitionerAwareUnionRDD,否则返回UnionRDD;

     PartitionerAwareUnionRDD 是oneToOneDependency 是一个宰依赖,会使用父RDD的分区器

     UnionRDD 中会判断RDD的个数是否超过了10个,没超过类似于合并了下分区,这个是自定义的依赖,传过来的依赖关系为Nil;计算依赖关系是使用的RangeDependency 也是一个宰依赖

RDD类

  /**
   * Return the union of this RDD and another one. Any identical elements will appear multiple
   * times (use `.distinct()` to eliminate them).
   */
  def union(other: RDD[T]): RDD[T] = withScope {
    sc.union(this, other)
  }
  /** Build the union of a list of RDDs passed as variable-length arguments. */
  def union[T: ClassTag](first: RDD[T], rest: RDD[T]*): RDD[T] = withScope {
    // TODO:Seq(first) ++ rest先将RDD放入到一个集合中
    union(Seq(first) ++ rest)
  }
  /** Build the union of a list of RDDs. */
  def union[T: ClassTag](rdds: Seq[RDD[T]]): RDD[T] = withScope {
    // TODO:过滤出 包含分区的rdd
    val nonEmptyRdds = rdds.filter(!_.partitions.isEmpty)
    // TODO:获取了所有RDD的分区器 并且去重
    val partitioners = nonEmptyRdds.flatMap(_.partitioner).toSet
    //TODO:如果每个RDD都设置了分区 并且是都是一样的 就使用此分区器整合所有的RDD
    if (nonEmptyRdds.forall(_.partitioner.isDefined) && partitioners.size == 1) {
      // TODO:返回PartitionerAwareUnionRDD  这个类继承了OneToOneDependency  所以这是一个宰依赖
      // 数据重新分区但是 是一个在依赖
      new PartitionerAwareUnionRDD(this, nonEmptyRdds)
    } else {
      // TODO:如果到这里是保留各自的分区了,返回UnionRDD
      new UnionRDD(this, nonEmptyRdds)
    }
  }

      PartitionerAwareUnionRDD类

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.spark.rdd

import java.io.{IOException, ObjectOutputStream}

import scala.reflect.ClassTag

import org.apache.spark.{OneToOneDependency, Partition, SparkContext, TaskContext}
import org.apache.spark.util.Utils

/**
 * Class representing partitions of PartitionerAwareUnionRDD, which maintains the list of
 * corresponding partitions of parent RDDs.
 */
private[spark]
class PartitionerAwareUnionRDDPartition(
    @transient val rdds: Seq[RDD[_]],
    override val index: Int
  ) extends Partition {
  var parents = rdds.map(_.partitions(index)).toArray

  override def hashCode(): Int = index

  override def equals(other: Any): Boolean = super.equals(other)

  @throws(classOf[IOException])
  private def writeObject(oos: ObjectOutputStream): Unit = Utils.tryOrIOException {
    // Update the reference to parent partition at the time of task serialization
    parents = rdds.map(_.partitions(index)).toArray
    oos.defaultWriteObject()
  }
}

/**
 * Class representing an RDD that can take multiple RDDs partitioned by the same partitioner and
 * unify them into a single RDD while preserving the partitioner. So m RDDs with p partitions each
 * will be unified to a single RDD with p partitions and the same partitioner. The preferred
 * location for each partition of the unified RDD will be the most common preferred location
 * of the corresponding partitions of the parent RDDs. For example, location of partition 0
 * of the unified RDD will be where most of partition 0 of the parent RDDs are located.
 */
private[spark]
class PartitionerAwareUnionRDD[T: ClassTag](
    sc: SparkContext,
    var rdds: Seq[RDD[T]]
                                           // TODO:这里是OneToOneDependency  是一个宰依赖
  ) extends RDD[T](sc, rdds.map(x => new OneToOneDependency(x))) {
  require(rdds.nonEmpty)
  require(rdds.forall(_.partitioner.isDefined))
  require(rdds.flatMap(_.partitioner).toSet.size == 1,
    "Parent RDDs have different partitioners: " + rdds.flatMap(_.partitioner))

  //TODO:因为所有RDD的分区器都是一样的,那么就使用第一个就可以了
  override val partitioner = rdds.head.partitioner

  override def getPartitions: Array[Partition] = {
    // TODO:整合RDD的分区
    val numPartitions = partitioner.get.numPartitions
    (0 until numPartitions).map { index =>
      // TODO:分区,PartitionerAwareUnionRDDPartition
      new PartitionerAwareUnionRDDPartition(rdds, index)
    }.toArray
  }

  // Get the location where most of the partitions of parent RDDs are located
  override def getPreferredLocations(s: Partition): Seq[String] = {
    logDebug("Finding preferred location for " + this + ", partition " + s.index)
    val parentPartitions = s.asInstanceOf[PartitionerAwareUnionRDDPartition].parents
    val locations = rdds.zip(parentPartitions).flatMap {
      case (rdd, part) =>
        val parentLocations = currPrefLocs(rdd, part)
        logDebug("Location of " + rdd + " partition " + part.index + " = " + parentLocations)
        parentLocations
    }
    val location = if (locations.isEmpty) {
      None
    } else {
      // Find the location that maximum number of parent partitions prefer
      Some(locations.groupBy(x => x).maxBy(_._2.length)._1)
    }
    logDebug("Selected location for " + this + ", partition " + s.index + " = " + location)
    location.toSeq
  }

  override def compute(s: Partition, context: TaskContext): Iterator[T] = {
    val parentPartitions = s.asInstanceOf[PartitionerAwareUnionRDDPartition].parents
    rdds.zip(parentPartitions).iterator.flatMap {
      case (rdd, p) => rdd.iterator(p, context)
    }
  }

  override def clearDependencies(): Unit = {
    super.clearDependencies()
    rdds = null
  }

  // Get the *current* preferred locations from the DAGScheduler (as opposed to the static ones)
  private def currPrefLocs(rdd: RDD[_], part: Partition): Seq[String] = {
    rdd.context.getPreferredLocs(rdd, part.index).map(tl => tl.host)
  }
}

     UnionRDD类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

best啊李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值