開始使用免費開始

SortByKey 與 Collect

有許多情況會需要依照鍵來排序成對的 RDD(例如本章稍後會看到的文字計數)。在這個練習中,你要將前一個練習建立的成對 RDD Rdd_Reduced 依鍵做遞減排序,並列印最終輸出。

請記得,你的工作環境中已提供 SparkContext scRdd_Reduced

本練習屬於課程

使用 PySpark 的 Big Data 基礎

檢視課程

練習說明

  • 使用鍵對 Rdd_Reduced 這個 RDD 進行遞減排序。
  • 收集內容並逐一列印輸出。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Sort the reduced RDD with the key by descending order
Rdd_Reduced_Sort = Rdd_Reduced.____(ascending=False)

# Iterate over the result and retrieve all the elements of the RDD
for num in Rdd_Reduced_Sort.____():
  print("Key {} has {} Counts".format(____, num[1]))
編輯並執行程式碼