天天看點

RDD的五大特性RDD(Resilient Distributed Dataset)

RDD(Resilient Distributed Dataset)

RDD是一個抽象類,它代表的是對不可變的分區元素的集合進行并行操作。

A list of partitions

一個RDD由幾個分區構成。它是一個可分區的集合,那麼它的好處就展現在,對于之前的普通不能進行分區的集合,資料就隻能在一個節點上進行處理,而對于RDD來說,對集合進行分區,那麼就可以把集合裡面的元素存儲在不同機器上處理。這樣性能肯定會有所提升。

/**
   * 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]

           

A function for computing each split/partition

這句話表示RDD具有并行處理任務的特性。每個函數會同時作用到幾個分區中,有幾個partition就有幾個task。對RDD做計算是相當于作用在每一個分片上的。對應函數compute

/**
   * :: DeveloperApi ::
   * Implemented by subclasses to compute a given partition.
   */
  @DeveloperApi
  def compute(split: Partition, context: TaskContext): Iterator[T]
 
           

A list of dependencies on other RDDs

假設對RDD1做了一個map操作,它得到的是RDD2,是一個新的RDD。每個RDD都依賴着其父類,可能來自于一個,也可能來自于多個。RDD會記錄它的依賴 ,這個特性是有容錯機制的,也就是說在記憶體中的RDD操作時出錯或丢失時能夠找到它的依賴關系來進行重算。對應函數getDependencies

/**
   * 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

           

Optionally, a Partitioner for key-value RDDs

RDD裡面的有一個分區器對應的是key-value,也就是說會有shuffled ,比如join、groupby這種。

/** Optionally overridden by subclasses to specify how they are partitioned. */
@transient val partitioner: Option[Partitioner] = None

           

補充:mapreduce的預設分區規則是根據key的hash對reduce的數量取模(key.hashCode()&Integer.MAX_VALUE)%numReduceTasks)

Optionally, a list of preferred locations to compute each split on

對于計算的時候,會優先把計算節點放在資料所在的block塊上面,如果機器繁忙的話,它會選擇等一會,如果不忙,那麼task就屬于是并行計算,同時做。假設說有一個檔案隻有一個block,它具有三個副本分别存儲在1、2、3三台機器上,那麼在計算的時候,它就會優先把計算節點啟動在1或者2或者3其中一台的機器上,如果這時候,這三個節點不是特别繁忙,那麼就會在這上面執行計算。假如說,這三台節點都比較繁忙,把計算節點放在4或者其它機器上,這時候其他機器并沒有這個block塊的内容,也就需要拷貝一份到現計算節點的機器上,那麼就很明顯不能保證資料本地性了。對于這種情況,一般都不會選擇把資料移動空閑的機器上去計算,都是把作業調到資料所在的機器上面,然後保持本地性操作。選擇移動計算而不是移動資料。

/**
   * Optionally overridden by subclasses to specify placement preferences.
   */
  protected def getPreferredLocations(split: Partition): Seq[String] = Nil

           

繼續閱讀