CountingBykeys
對許多資料集來說,計算鍵/值資料集中鍵的數量很重要。例如果品在多少國家銷售,或找出最受歡迎的嬰兒名字。在這個簡單的練習中,你會使用先前建立的 Rdd,並計算該 pair RDD 中唯一鍵的數量。
請記住,你的工作環境中已經有 SparkContext sc 和 Rdd 可用。
本練習屬於課程
使用 PySpark 的 Big Data 基礎
練習說明
- 使用
countByKey,並將結果指定給變數total。 total的型別是什麼?- 走訪
total,並列印每個鍵及其出現次數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Count the unique keys
total = Rdd.____()
# What is the type of total?
print("The type of total is", ____(total))
# Iterate over the total and print the output
for k, v in total.___():
print("key", ____, "has", ____, "counts")