1. 程式人生 > >spark rdd checkpoint的用法注意點

spark rdd checkpoint的用法注意點

 /**
   * Mark this RDD for checkpointing. It will be saved to a file inside the checkpoint
   * directory set with `SparkContext#setCheckpointDir` and all references to its parent
   * RDDs will be removed. This function must be called before any job has been
   * executed on this RDD. It is strongly recommended that this RDD is persisted in
   * memory, otherwise saving it on a file will require recomputation.

   */

這是原始碼中RDD裡的checkpoint()方法的註釋,裡面建議在執行checkpoint()方法之前先對rdd進行persisted操作。

為啥要這樣呢?因為checkpoint會觸發一個Job,如果執行checkpoint的rdd是由其他rdd經過許多計算轉換過來的,如果你沒有persisted這個rdd,那麼又要重頭開始計算該rdd,也就是做了重複的計算工作了,所以建議先persist rdd然後再checkpoint,checkpoint會丟棄該rdd的以前的依賴關係,使該rdd成為頂層父rdd,這樣在失敗的時候恢復只需要恢復該rdd,而不需要重新計算該rdd了,這在迭代計算中是很有用的,假設你在迭代1000次的計算中在第999次失敗了,然後你沒有checkpoint,你只能重新開始恢復了,如果恰好你在第998次迭代的時候你做了一個checkpoint,那麼你只需要恢復第998次產生的rdd,然後再執行2次迭代完成總共1000的迭代,這樣效率就很高,比較適用於迭代計算非常複雜的情況,也就是恢復計算代價非常高的情況,適當進行checkpoint會有很大的好處。