6. RDD综合练习:更丰富的操作

一、集合运算练习

union()
image
intersection()
image
subtract()
image
cartesian()
image

二、内连接与外连接

join()
image
leftOuterJoin()
image
rightOuterJoin()
image
fullOuterJoin()
image

三、综合练习:学生课程分数

网盘下载sc.txt文件,通过RDD操作实现以下数据分析:

  • 持久化 scm.cache()

image

image

image

  • 总共有多少学生?map(), distinct(), count()

image

  • 开设了多少门课程?

image

  • 生成(姓名,课程分数)键值对RDD,观察keys(),values()

image

  • 每个学生选修了多少门课?map(), countByKey()

image

  • 每门课程有多少个学生选?map(), countByValue()

image

  • 有多少个100分?

image

  • Tom选修了几门课?每门课多少分?filter(), map() RDD

image

image

  • Tom选修了几门课?每门课多少分?map(),lookup() list

image
image

  • Tom的成绩按分数大小排序。filter(), map(), sortBy()

image

  • Tom的平均分。map(),lookup(),mean()

image

  • 生成(姓名课程,分数)RDD,观察keys(),values()

image

  • 每个分数+20分。分别用mapValues(func)和 map(func)实现。并查看不及格人数的变化。

image

  • 求每门课的选修人数及所有人的总分。combineByKey()

image

  • 求每门课的选修人数及平均分,精确到2位小数。map(),round()

image

  • 求每门课的选修人数及平均分。用reduceByKey()实现,并比较与combineByKey()的异同。

image

reduceByKey 用于对每个 key 对应的多个 value 进行 merge 操作,最重要的是它能够在本地先进行 merge 操作,并且 merge 操作可以通过函数自定义;

combineByKey是一个比较底层的算子
1.createCombiner():在遍历过程中,遇到新的键,就会调用createCombiner()函数。这个过程会发生在每一个分区内,因为RDD中有不同的分区,也就有同一个键调用多次createCombiner的情况。
2.mergeValue() 遇到已经重复的键,调用mergeValue()函数。
3.mergeCombiners() 如果有2个或者更多的分区,会把分区的结果合并。
4.pationer 分区函数()

  • 结果可视化。 pyecharts.charts,Bar()
posted @ 2022-06-06 12:17  空木蓮華  阅读(13)  评论(0编辑  收藏  举报