RDD 即 Resilient Distributes Dataset, 是spark中最基础、最常用的数据结构。其本质是把input source 进行封装,封装之后的数据结构就是RDD,提供了一系列操作,比如 map、flatMap、filter等。input source种类繁多,比如hdfs上存储的文件、本地存储的文件,相应的 RDD的种类也有很多。不同的input source 对应着不同的rdd 类型。比如从hdfs上读取的text对应着HadoopRDD, val hRdd= sc.textFile(“hfs://…….”) 生成的就是HadoopRDD。


深入理解RDD
  如图所示,RDD是一个父类,是一个抽象类,封装了rdd的基本属性和常用操作。每一个具体的子类rdd都继承了该父类。

  rdd包含五个特征:
  1. 一个分片列表 partition list
  2. 一个计算函数compute,对每一个split进行计算
  3. 对其他rdd的依赖列表dependencies list.依赖又份 宽依赖和窄依赖。
  4. partitioner for key-value RDDs.比如说 hash-partitioned rdd(这是可选的,并不是所有的add都会有这个特征)
  5. 对每一个split计算的优先位置 Preferred Location。比如对一个hdfs文件进行计算时,可以获取优先计算的block locations
  以上五个特征中包含四个函数和一个属性,如下所示:
  1. protected def getPartitions: Array[Partition]     //only called once
  2. def compute(split: Partition, context: TaskContext): Iterator[T]
  3. protected def getDependencies: Seq[Dependency[_]] = dips    //only called once
  4. protected def getPreferredLocations(split: Partition): Seq[String] = Nil
  5. @transient val partitioner: Option[Partitioner] = None
    这五个特征的定义包含在抽象父类RDD中(对应于上图中的RDD,具体的代码在RDD.scala中),每一个具体类型的rdd子类继承RDD父类之后都会部分或全部override这五个特征。如MapPartitionsRDD就重写了其中的三个:
  • override val partitioner if (preservesPartitioning) firstParent[T].partitioner else None
  • override def getPartitions: Array[Partition] = firstParent[T].partitions
  • override def compute(split: Partition, context: TaskContext): Iterator[U] = f(context, split.index, firstParent[T].iterator(split, context))

相关文章:

  • 2022-12-23
  • 2021-05-27
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2021-11-08
  • 2021-12-10
猜你喜欢
  • 2021-08-22
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-08-19
相关资源
相似解决方案