CountingBykeys
对于许多数据集,统计键/值数据集中键的数量非常重要。例如,统计产品售出的国家数,或找出最受欢迎的婴儿姓名。在这个简单练习中,您将使用之前创建的 Rdd,并统计该键值对 RDD 中唯一键的数量。
请记住,您的工作区中已经提供了 SparkContext sc 和 Rdd。
本练习是课程的一部分
使用 PySpark 的大数据基础
练习说明
- 使用
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")