介绍
RDD(Resilient Distributed Dataset)叫做 弹性分布式数据集,是Spark中最基本的数据抽象,是 Spark 中 最基本 的 数据处理模型。
代码中是一个抽象类,它代表一个 弹性的、不可变、可分区、里面的元素可并行计算的集合。
- 弹性:
- 存储的弹性:内存与磁盘的自动切换;
- 容错的弹性:数据丢失可以自动恢复;
- 计算的弹性:计算出错重试机制;
- 分片的弹性:可根据需要重新分片。
- 分布式:数据存储在大数据集群不同节点上
- 数据集:RDD 封装了计算逻辑,并不保存数据
- 数据抽象:RDD 是一个抽象类,需要子类具体实现
- 不可变:RDD 封装了计算逻辑,是不可以改变的,想要改变,只能产生新的 RDD,在新的 RDD 里面封装计算逻辑
- 可分区、并行计算
- 泛型:支持各种数据类型
五大特性
A list of partitions
RDD由很多partition构成,在spark中,计算式,有多少partition就对应有多少个task来执行A function for computing each split
对RDD做计算,相当于对RDD的每个split或partition做计算A list of dependencies on other RDDs
RDD之间有依赖关系,可溯源Optionally, a Partitioner for key-value RDDs (e.g. to say that the RDD is hash-partitioned)
如果RDD里面存的数据是key-value形式,则可以传递一个自定义的Partitioner进行重新分区,比如可以按key的hash值分区Optionally, a list of preferred locations to compute each split on (e.g. block locations for an HDFS file)
最优的位置去计算,也就是数据的本地性
计算每个split时,在split所在机器的本地上运行task是最好的,避免了数据的移动;split有多个副本,所以preferred location不止一个
数据在哪里,应优先把作业调度到数据所在机器上,减少数据的IO和网络传输,这样才能更好地减少作业运行时间(木桶原理:作业运行时间取决于运行最慢的task所需的时间),提高性能
源码中的体现
特性1
getPartitions() 返回的必然是一系列Partition类型的数据组成的数组
/**
* Implemented by subclasses to return the set of partitions in this RDD. This method will only
* be called once, so it is safe to implement a time-consuming computation in it.
*
* The partitions in this array must satisfy the following property:
* `rdd.partitions.zipWithIndex.forall { case (partition, index) => partition.index == index }`
*/
protected def getPartitions: Array[Partition]
特性2
compute()函数的入参必然是partition,因为对RDD做计算相当于对每个partition做计算
/**
* :: DeveloperApi ::
* Implemented by subclasses to compute a given partition.
*/
@DeveloperApi
def compute(split: Partition, context: TaskContext): Iterator[T]
特性3
RDD之间有依赖关系
/**
* Implemented by subclasses to return how this RDD depends on parent RDDs. This method will only
* be called once, so it is safe to implement a time-consuming computation in it.
*/
protected def getDependencies: Seq[Dependency[_]] = deps
特性4
/** Optionally overridden by subclasses to specify how they are partitioned. */
@transient val partitioner: Option[Partitioner] = None
特性5
/**
* Optionally overridden by subclasses to specify placement preferences.
*/
protected def getPreferredLocations(split: Partition): Seq[String] = Nil