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

一、集合运算练习

union()intersection()subtract(), cartesian()

rdd1 = sc.parallelize('abcd')
rdd2 = sc.parallelize('bcde')

rdd1.union(rdd2).collect()

rdd1.subtract(rdd2).collect()

rdd1.intersection(rdd2).collect()

rdd1.cartesian(rdd2).collect()

二、内连接与外连接

rdd6 = sc.textFile('file:///home/hadoop/kq06.txt').map(lambda line:(line,'06'))
rdd6.collect()

rdd7 = sc.textFile('file:///home/hadoop/kq07.txt').map(lambda line:(line,'07'))
rdd7.collect()

多个考勤文件,签到日期汇总

使用join(), leftOuterJoin(), rightOuterJoin(), fullOuterJoin()

rdd6.join(rdd7).collect()

rdd6.leftOuterJoin(rdd7).collect()

rdd6.rightOuterJoin(rdd7).collect()

rdd6.fullOuterJoin(rdd7).collect()

出勤次数统计

rdd3 = sc.textFile('file:///home/hadoop/kq06.txt').map(lambda line:(line,1))
rdd4 = sc.textFile('file:///home/hadoop/kq07.txt').map(lambda line:(line,1))

rdd3.union(rdd4).collect()

rdd3.union(rdd4).reduceByKey(lambda a,b:a+b).collect()

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

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

  • 持久化 scm.cache()
url = 'file:///home/hadoop/下载/sc.txt'
scm = sc.textFile(url).map(lambda line:line.split(',')).map(lambda line:[line[0],line[1],int(line[2])])
scm.cache()

scm.take(3)

  • 总共有多少学生?开设了多少门课程?map(), distinct(), count()
scm.count()
scm.map(lambda line:line[0]).distinct().count()
scm.map(lambda line:line[1]).distinct().count()   # 开设几门课程

  • 生成(姓名,课程分数)键值对RDD
name = scm.map(lambda line:(line[0],(line[1],line[2])))
name.take(3)

  • 观察 keys(), values()
name.keys().take(6)
name.values().take(6)

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

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

  • 有多少个100分?
name.values().values().countByValue()[100]

name.take(1)
name.values().take(1)
name.values().values().take(1)

  • Tom选修了几门课?每门课多少分?filter(), map() RDD
scm.filter(lambda line:line[0]=='Tom').map(lambda line:line[1]).collect()
scm.filter(lambda line:line[0]=='Tom').map(lambda line:line[1]).count()

scm.filter(lambda line:line[0]=='Tom').collect()

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

name.lookup('Tom')

  • Tom的成绩按分数大小排序。filter(), map(), sortBy()
name.filter(lambda line:line[0]=='Tom').values().collect()
name.filter(lambda line:line[0]=='Tom').values().sortBy(lambda a:a[1], False).collect()

  • Tom的平均分。map(),lookup(),mean()
import numpy as np
np.mean(scm.filter(lambda line:line[0]=='Tom').map(lambda line:line[2]).collect())

  • 生成(姓名课程,分数)RDD,观察 keys(), values()
score = scm.map(lambda line:((line[0],line[1]),line[2]))
score.take(3)

  • 每个分数+20平时分,分别用mapValues(func)map(func)实现,并查看不及格人数的变化
scm.filter(lambda line:line[2]<60).count()

adjust = score.mapValues(lambda v:v+20)
adjust.first()
adjust.filter(lambda line:line[1]<60).count()

score = scm.map(lambda line:((line[0],line[1]),line[2]*0.8+20))
score.filter(lambda line:line[1]<60).count()

  • 求每门课的平均分

    1. lookup(), np.mean()实现

      course.lookup('OperatingSystem')
      np.mean(list(map(int,course.lookup('OperatingSystem'))))
      

    2. reduceByKey()collectAsMap()实现

      course = scm.map(lambda line:(line[1],(line[2],1)))
      result = course.reduceByKey(lambda x,y:(x[0]+y[0],x[1]+y[1])).map(lambda a:(a[0],a[1][1],round(a[1][0]/a[1][1], 2))).collect()
      result
      

      result = course.reduceByKey(lambda x,y:(x[0]+y[0],x[1]+y[1])).map(lambda a:(a[0],round(a[1][0]/a[1][1], 2))).collectAsMap()
      result
      

    3. combineByKey()map()round()实现,确到2位小数

      course=scm.map(lambda line:(line[1],line[2]))
      course.first()
      courseC=course.combineByKey(
      	lambda v: (int(v),1), 
      	lambda c,v:(c[0]+int(v),c[1]+1),
      	lambda c1,c2:(c1[0]+c2[0],c1[1]+c2[1]))
      courseC.collect()
      courseC.map(lambda x:(x[0],x[1][1],round(x[1][0]/x[1][1], 2))).collect()
      

posted @ 2022-04-20 17:43  stu(dying)  阅读(62)  评论(0编辑  收藏  举报