1.. 简介

  spark从RDD依赖上来说分为窄依赖和宽依赖。

其中可以这样区分是哪种依赖:当父RDD的一个partition被子RDD的多个partitions引用到的时候则说明是宽依赖,否则为窄依赖。

宽依赖会触发shuffe,宽依赖也是一个job钟不同stage的分界线。

本篇文章主要讨论一下窄依赖的场景。

2.依赖关系的建立

  字RDD内部维护着父RDD的依赖关系,下列是依赖的抽象类,其中属性rdd就是父RDD

/**
* :: DeveloperApi ::
* Base class for dependencies.
*/
@DeveloperApi
abstract class Dependency[T] extends Serializable {
def rdd: RDD[T]
}

  

3.窄依赖的三种形式:

  窄依赖的抽象类如下:

/**
* :: DeveloperApi ::
* Base class for dependencies where each partition of the child RDD depends on a small number
* of partitions of the parent RDD. Narrow dependencies allow for pipelined execution.
*/
@DeveloperApi
abstract class NarrowDependency[T](_rdd: RDD[T]) extends Dependency[T] {
/**
* 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] override def rdd: RDD[T] = _rdd
}

  窄依赖形式一:MAP,Filter....

如上两个RDD的转换时通过MAP或者Filter等转换的,RDD的各个partition都是一一对应的,从执行时可以并行化的。

子RDD的分区依赖的父RDD的分区ID是一样不会有变化,这样的窄依赖实现类如下:

/**
* :: DeveloperApi ::
* Represents a one-to-one dependency between partitions of the parent and child RDDs.
*/
@DeveloperApi
class OneToOneDependency[T](rdd: RDD[T]) extends NarrowDependency[T](rdd) {
override def getParents(partitionId: Int): List[Int] = List(partitionId) //子RDD的某个分区ID是和父RDD的分区ID是一致的
}

  

  窄依赖方式二: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
* @param outStart the start of the range in the 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): List[Int] = {
if (partitionId >= outStart && partitionId < outStart + length) {
List(partitionId - outStart + inStart)
} else {
Nil
}
}

一开始并不好理解上述代码,可参考下图,下图中将各个参数的意义图形化展示:

所以上述中子RDD分区中的位号(partitionid)和父RDD的位置号(partitionid)相对的差值 (outStart-inStart)

if (partitionId >= outStart && partitionId < outStart + length) 这段代码的意义:检查当前子RDD分区ID是否在当前父RDD下的范围内
partitionId - outStart + inStart 的意思是:当前子RDD分区id(位置号)与差值相减得出其在父RDD上的分区位置号(id)其实就是:partitionId - (outStart-inStart)

窄依赖方式三:join with inputs co-partitioned
此场景适用于窄依赖方式一。
 

最新文章

  1. C# 将文件转化成byte[]数组
  2. mysql安装配置问题(linux下)
  3. 自定义input[type=&quot;radio&quot;]的样式
  4. 读《深入理解Java虚拟机》有感——第二部分:虚拟机类加载机制
  5. oracle 序列 详解
  6. Difference Between Vector and Deque in C++
  7. 深入浅出Java并发包—锁机制(二)
  8. Android视图SurfaceView的实现原理分析
  9. BZOJ_1601_[Usaco2008_Oct]_灌水_(最小生成树_Kruskal)
  10. Linux查看系统信息(CentOS 7中测试通过)
  11. Java并发之CyclicBarrier工具类
  12. 第一课android开发之在activity间传递参数
  13. python的函数学习2
  14. 发送Http
  15. 安全测试===sqlmap(壹)转载
  16. Linux下安装PHP的lua扩展库
  17. 【LOJ】#2040. 「SHOI2015」零件组装机
  18. 将oh-my-zsh编程真正的my zsh
  19. Sqli-labs介绍、下载、安装
  20. 利用github搭建个人网站

热门文章

  1. AtCoder Grand Contest 003
  2. Linux使用vim进行多文件查找和替换的方法
  3. WEB入门 四 CSS样式表深入
  4. STS导入Gradle项目出现 Could not create task of type &#39;DependencyManagementReportTask&#39;
  5. k-Nearest Neighbor algorithm 思想
  6. 题解【bzoj4587 &amp; bzoj4408 [FJOI2016]神秘数】
  7. Docker部署Tomcat实例
  8. python---协程理解
  9. Dubbo学习笔记1:使用Zookeeper搭建服务治理中心
  10. SQL语句(二十一)—— 触发器(DML触发器)