1、Spark中採用依赖关系(Dependency)表示rdd之间的生成关系。Spark可利用Dependency计算出失效的RDD。在每一个RDD中都存在一个依赖关系的列表

  private var dependencies_ : Seq[Dependency[_]] = null

用以记录各rdd中各partition的parent partition。

2、Spark中存在两类Dependency:

1)NarrowDependency表示的是一个父partition仅相应于一个子partition。这种依赖关系是不须要shuffle的。在这类依赖中。能够依据getParents方法获取某个partition的父partitions:

/**
* :: DeveloperApi ::
* Base class for dependencies where each partition of the parent RDD is used by at most one
* partition of the child RDD. Narrow dependencies allow for pipelined execution.
*/
@DeveloperApi
abstract class NarrowDependency[T](rdd: RDD[T]) extends Dependency(rdd) {
/**
* 唯一的接口。获得该partition的全部parent partition
* Get the parent partitions for a child partition.
* @param partitionId a partition of the child RDD
* @return the partitions of the parent RDD that the child partition depends upon
*/
def getParents(partitionId: Int): Seq[Int]
}

这类又可分为:

a、OneToOneDependency:表示一一相应的依赖关系,因为在这样的依赖中父partition与子partition Id是一致的,所以getParents直接原样返回。相应的转换操作有map和filter

class OneToOneDependency[T](rdd: RDD[T]) extends NarrowDependency[T](rdd) {
/**
* 事实上partitionId就是partition在RDD中的序号, 所以假设是一一相应, 那么parent和child中的partition的序号应该是一样的
*/
override def getParents(partitionId: Int) = List(partitionId)//原样返回
}

b、PruneDependency(org.apache.spark.rdd.PartitionPruningRDDPartition):未详

/**
* Represents a dependency between the PartitionPruningRDD and its parent. In this
* case, the child RDD contains a subset of partitions of the parents'.
*/
private[spark] class PruneDependency[T](rdd: RDD[T], @transient partitionFilterFunc: Int => Boolean)
extends NarrowDependency[T](rdd) { @transient
val partitions: Array[Partition] = rdd.partitions
.filter(s => partitionFilterFunc(s.index)).zipWithIndex
.map { case(split, idx) => new PartitionPruningRDDPartition(idx, split) : Partition } override def getParents(partitionId: Int) = {
List(partitions(partitionId).asInstanceOf[PartitionPruningRDDPartition].parentSplit.index)
}
}

c、RangeDependency:这样的是父rdd的连续多个partitions相应子rdd中的连续多个partitions。相应的转换有union

/**Union
* :: DeveloperApi ::
* Represents a one-to-one dependency between ranges of partitions in the parent and child RDDs.
* @param rdd the parent RDD
* @param inStart the start of the range in the parent RDD parent RDD中区间的起始点
* @param outStart the start of the range in the child RDD child RDD中区间的起始点
* @param length the length of the range
*/
@DeveloperApi
class RangeDependency[T](rdd: RDD[T], inStart: Int, outStart: Int, length: Int)
extends NarrowDependency[T](rdd) { override def getParents(partitionId: Int) = {
if (partitionId >= outStart && partitionId < outStart + length) {//推断partitionId的合理性,必须在child RDD的合理partition范围
List(partitionId - outStart + inStart)//算出parent RDD中相应的partition id
} else {
Nil
}
}
}

2)WideDependency:这样的依赖是指一个父partition能够相应子rdd中多个partitions。因为须要对父partition进行划分,故须要用到shuffle,而shuffle通常是採用键值对的。

这里为每一个shuffle分配了一个全局唯一的shuffleId。

为了进行shuffle。须要指定怎样进行shuffle,这相应于參数partitioner;因为shuffle是须要网络传输的。故须要进行序列化Serializer。在宽依赖中并无法获得partition相应的parent partitions?

/**
* :: DeveloperApi ::
* Represents a dependency on the output of a shuffle stage.
* @param rdd the parent RDD
* @param partitioner partitioner used to partition the shuffle output
* @param serializer [[org.apache.spark.serializer.Serializer Serializer]] to use. If set to null,
* the default serializer, as specified by `spark.serializer` config option, will
* be used.
*/
@DeveloperApi
class ShuffleDependency[K, V](
@transient rdd: RDD[_ <: Product2[K, V]],
val partitioner: Partitioner,//须要给出partitioner, 指示怎样完毕shuffle
val serializer: Serializer = null)//shuffle不象map能够在local进行, 往往须要网络传输或存储, 所以须要serializerClass
extends Dependency(rdd.asInstanceOf[RDD[Product2[K, V]]]) { val shuffleId: Int = rdd.context.newShuffleId()//每一个shuffle须要分配一个全局的id, context.newShuffleId()的实现就是把全局id累加 rdd.sparkContext.cleaner.foreach(_.registerShuffleForCleanup(this))
}

Spark-Dependency的更多相关文章

  1. Spark快速入门 - Spark 1.6.0

    Spark快速入门 - Spark 1.6.0 转载请注明出处:http://www.cnblogs.com/BYRans/ 快速入门(Quick Start) 本文简单介绍了Spark的使用方式.首 ...

  2. 在 Azure HDInsight 中安装和使用 Spark

    Spark本身用Scala语言编写,运行于Java虚拟机(JVM).只要在安装了Java 6以上版本的便携式计算机或者集群上都可以运行spark.如果您想使用Python API需要安装Python解 ...

  3. spark mllib配置pom.xml错误 Multiple markers at this line Could not transfer artifact net.sf.opencsv:opencsv:jar:2.3 from/to central (https://repo.maven.apache.org/maven2): repo.maven.apache.org

    刚刚spark mllib,在maven repository网站http://mvnrepository.com/中查询mllib后得到相关库的最新dependence为: <dependen ...

  4. Spark实战3:Maven_Java_HelloWorld

    Spark独立开发应用( Java语言) 1 创建SimpleApp.java文件: /* SimpleApp.java */ import org.apache.spark.api.java.*; ...

  5. Spark RDD概念学习系列之RDD的转换(十)

    RDD的转换 Spark会根据用户提交的计算逻辑中的RDD的转换和动作来生成RDD之间的依赖关系,同时这个计算链也就生成了逻辑上的DAG.接下来以“Word Count”为例,详细描述这个DAG生成的 ...

  6. Spark RDD概念学习系列之rdd的依赖关系彻底解密(十九)

    本期内容: 1.RDD依赖关系的本质内幕 2.依赖关系下的数据流视图 3.经典的RDD依赖关系解析 4.RDD依赖关系源码内幕 1.RDD依赖关系的本质内幕 由于RDD是粗粒度的操作数据集,每个Tra ...

  7. Spark IDEA开发环境构建

    本文档基于IEDA构建spark maven应用. date: 2016/8/1 author: wangxl 1.下载IDEA https://www.jetbrains.com/idea/ 2.安 ...

  8. CentOS7 安装spark集群

    Spark版本 1.6.0 Scala版本 2.11.7 Zookeeper版本 3.4.7 配置虚拟机 3台虚拟机,sm,sd1,sd2 1. 关闭防火墙 systemctl stop firewa ...

  9. Spark 2.2.0 文档中文版 Quick Start

    原地址:http://spark.apache.org/docs/latest/quick-start.html 这篇指导对使用Spark提供了一个快速的介绍.我们首先介绍API,通过spark交互式 ...

  10. Spark快速入门

    Spark 快速入门   本教程快速介绍了Spark的使用. 首先我们介绍了通过Spark 交互式shell调用API( Python或者scala代码),然后演示如何使用Java, Scala或者P ...

随机推荐

  1. 初见Python<5>:条件、循环和其他语句

    1.使用逗号输出 使用逗号隔开,可以打印多个表达式.打印后,各项之间自动以一个空格隔开. 也可以同时输出文本和变量值. 可以和字符串连接符“+”一起使用.   2.从模块中导入函数 从模块导入函数的方 ...

  2. luogu P1137 旅行计划

    题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发,并只往东走到城市i停止. 所以他就需要选择最先到达的城市,并制定一条路线以城市i为终 ...

  3. [AGC012E]Camel and Oases

    题意:有$n$个数轴上的绿洲,给定它们的坐标,有一只骆驼想要访问所有绿洲,当它的驼峰容量为$V$时,它可以走到和当前绿洲距离$\leq V$的绿洲,并可以继续走,它也可以用一次跳跃到达任意一个绿洲,只 ...

  4. 【构造】AtCoder Regular Contest 079 F - Namori Grundy

    对每个点的取值都取最小的可能值. 那个图最多一个环,非环的点的取值很容易唯一确定. 对于环上的点v,其最小可能取值要么是mex{c1,c2,...,ck}(ci这些是v直接相连的非环点)(mex是). ...

  5. 【动态规划+高精度】mr360-定长不下降子序列

    [题目大意] 韵哲君发现自己的面前有一行数字,当她正在琢磨应该干什么的时候,这时候,陈凡老师从天而降,走到了韵哲君的身边,低下头,对她耳语了几句,然后飘然而去. 陈凡老师说了什么呢,陈凡老师对韵哲君说 ...

  6. kosaraju算法求强连通分量

    什么是强连通分量?在这之前先定义一个强连通性(strong connectivity)的概念:有向图中,如果一个顶点s到t有一条路径,t到s也有一条路径,即s与t互相可达,那么我们说s与t是强连通的. ...

  7. Android工具:Hierarchy Viewer

    Hierarchy Viewer 用途: 即可以用来优化自己的布局,也可以用来参考别人优秀的布局 打开方式: 运行工程,然后在\android-sdk-windows\tools目录下双击hierar ...

  8. 域名做CDN来通过隐藏服务器真实IP的方法来防止DDoS攻击(转)

    隐藏服务器真实IP是解决问题最好和最快的方法,但只针对小流量,大流量同样会扛不住. 服务器前端加CDN中转,比如阿里云.百度云加速.360网站卫士.加速乐.安全宝等,如果资金充裕的话,可以购买高防的盾 ...

  9. hdu2263Heavy Cargo

    #include <iostream> #include <cstdio> #include <algorithm> #include <queue>/ ...

  10. __FILE__,__LINE__,FUNCTION__实现代码跟踪调试

    转:http://www.cnitblog.com/zouzheng/archive/2007/08/31/32691.aspx 先看下简单的初始代码:注意其编译运行后的结果. root@xuanfe ...