Spark RDD 窄依赖研究
2024-08-27 19:00:23
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
此场景适用于窄依赖方式一。
最新文章
- C# 将文件转化成byte[]数组
- mysql安装配置问题(linux下)
- 自定义input[type=";radio";]的样式
- 读《深入理解Java虚拟机》有感——第二部分:虚拟机类加载机制
- oracle 序列 详解
- Difference Between Vector and Deque in C++
- 深入浅出Java并发包—锁机制(二)
- Android视图SurfaceView的实现原理分析
- BZOJ_1601_[Usaco2008_Oct]_灌水_(最小生成树_Kruskal)
- Linux查看系统信息(CentOS 7中测试通过)
- Java并发之CyclicBarrier工具类
- 第一课android开发之在activity间传递参数
- python的函数学习2
- 发送Http
- 安全测试===sqlmap(壹)转载
- Linux下安装PHP的lua扩展库
- 【LOJ】#2040. 「SHOI2015」零件组装机
- 将oh-my-zsh编程真正的my zsh
- Sqli-labs介绍、下载、安装
- 利用github搭建个人网站
热门文章
- AtCoder Grand Contest 003
- Linux使用vim进行多文件查找和替换的方法
- WEB入门 四	CSS样式表深入
- STS导入Gradle项目出现 Could not create task of type &#39;DependencyManagementReportTask&#39;
- k-Nearest Neighbor algorithm 思想
- 题解【bzoj4587 &; bzoj4408 [FJOI2016]神秘数】
- Docker部署Tomcat实例
- python---协程理解
- Dubbo学习笔记1:使用Zookeeper搭建服务治理中心
- SQL语句(二十一)—— 触发器(DML触发器)