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. javascrip异步问题

    for ( var i = 1; i <= 3; i++) { setTimeout( function (){ console.log(i); }, 0); };     一般人会以为输出结果 ...

  2. 【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string

    题意:交互题:存在一个至少有一个0和一个1的长度为n的二进制串,你可以进行最多15次询问,每次给出一个长度为n的二进制串,系统返回你此串和原串的海明距离(两串不同的位数).最后要你找到任意一个0的位置 ...

  3. (原创)Stanford Machine Learning (by Andrew NG) --- (week 6) Advice for Applying Machine Learning & Machine Learning System Design

    (1) Advice for applying machine learning Deciding what to try next 现在我们已学习了线性回归.逻辑回归.神经网络等机器学习算法,接下来 ...

  4. Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树

    D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...

  5. Web安全测试指南--文件系统

    上传: 编号 Web_FileSys_01 用例名称 上传功能测试 用例描述 测试上传功能是否对上传的文件类型做限制. 严重级别 高 前置条件 1.  目标web应用可访问,业务正常运行. 2.  目 ...

  6. Promise小结

    Promise是异步里面的一种解决方案,解决了回调嵌套的问题,es6将其进行了语言标准,同意了用法,提供了`promise`对象, promise对象有三种状态:pending(进行中) .Resol ...

  7. Afreechart很强大的图表库,支持股票曲线图,饼图,曲线

    Afreechart是一个很强大的图表库,支持股票曲线图,饼图,曲线等.源码下载:http://www.23code.com/afreechart/

  8. mysql 碎片清理

    在MySQL中,我们经常会使用VARCHAR.TEXT.BLOB等可变长度的文本数据类型.不过,当我们使用这些数据类型之后,我们就不得不做一些额外的工作——MySQL数据表碎片整理. 那么,为什么在使 ...

  9. Spring Dataflow批处理框架在OCP上的部署

    详细参考 https://donovanmuller.blog/spring-cloud-dataflow-server-openshift/docs/1.2.1.RELEASE/reference/ ...

  10. docker部署golang+redis聊天室

    博客地址:http://www.niu12.com/article/7#####1.项目源码: https://github.com/ZQCard/webchat#####2.项目构成 websock ...