SortByKey 和 Collect
很多时候,需要按键对二元组 RDD 进行排序(例如本章后面将看到的词频统计)。在本练习中,您将把上一题中创建的二元组 RDD Rdd_Reduced 按降序排序,并打印最终输出。
请注意,工作区中已经提供了 SparkContext sc 和 Rdd_Reduced。
本练习是课程的一部分
使用 PySpark 的大数据基础
练习说明
- 使用键对
Rdd_Reduced按降序排序。 - 收集其内容并迭代打印输出。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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]))