08 学生课程分数的Spark SQL分析
读学生课程分数文件chapter4-data01.txt,创建DataFrame。
1.生成“表头”
2.生成“表中的记录”
3.把“表头”和“表中的记录”拼装在一起
用DataFrame的操作或SQL语句完成以下数据分析要求,并和用RDD操作的实现进行对比:
一、DataFrame的操作
- 每个分数+5分。
-
df_scs.select('name','cource',df_scs['score']+5).show()
- 总共有多少学生?
-
df_scs.select('name').distinct().count()
- 总共开设了哪些课程?
- df_scs.select('cource').distinct().show()
- 每个学生选修了多少门课?
- df_scs.groupBy('name').count().show()
- 每门课程有多少个学生选?
- df_scs.groupBy('cource').count().show()
- 每门课程大于95分的学生人数?
- df_scs.filter(df_scs['score']>95).groupBy('cource').count().show()
- Tom选修了几门课?每门课多少分?
- df_scs.filter(df_scs['name']=='Tom').show()
- Tom的成绩按分数大小排序。
- df_scs.filter(df_scs['name']=='Tom').sort(df_scs['score'].desc()).show()
- Tom的平均分。
- df_scs.filter(df_scs['name']=='Tom').agg({'score':'mean'}).show()
- 求每门课的平均分,最高分,最低分。
- 每门课的平均分:
- df_scs.groupBy("cource").avg('score').show()
-
df_scs.groupBy("cource").agg({'score':'mean'}).show()
- 每门课的最高分:
-
df_scs.groupBy("cource").max('score').show()
df_scs.groupBy("cource").agg({'score':'max'}).show()
- 每门课的最低分:
-
df_scs.groupBy("cource").min('score').show()
df_scs.groupBy("cource").agg({'score':'min'}).show()
- 求每门课的选修人数及平均分,精确到2位小数。
- from pyspark.sql.types import DecimalType
-
dt = df_scs.groupBy('cource').count().join(df_scs.groupBy('cource').agg({'score':'mean'}),'cource').withColumnRenamed('avg(score)','avg_score')
- dt.withColumn('avg_score',dt.avg_score.cast(DecimalType(5,2))).show()
- 每门课的不及格人数,通过率
- df_scs.filter(df_scs.score<60).groupBy(df_scs.cource).count().show()
- 结果可视化。
二、用SQL语句完成以下数据分析要求
必须把DataFrame注册为临时表
df_scs.createOrReplaceTempView('scs')
- 每个分数+5分。
- spark.sql("SELECT name,course,score+5 from scs").show()
- 总共有多少学生?
- spark.sql("SELECT count(name) from scs").show()
- 总共开设了哪些课程?
- spark.sql("SELECT distinct(course) from scs").show()
- 每个学生选修了多少门课?
- spark.sql("SELECT name,count(course) from scs group by name").show()
- 每门课程有多少个学生选?
- spark.sql("SELECT count(name),course from scs group by course").show()
- 每门课程大于95分的学生人数?
- spark.sql("SELECT count(name),course from scs where score>95 group by course").show()
- Tom选修了几门课?每门课多少分
- spark.sql("SELECT count(course) from scs where name='Tom'").show()
- spark.sql("SELECT course,score from scs where name='Tom'").show()
- Tom的成绩按分数大小排序。
- (从大到小 desc 从小到大 asc)
- spark.sql("SELECT course,score from scs where name='Tom' order by score desc").show()
- Tom的平均分。
- spark.sql("SELECT avg(score) from scs where name='Tom'").show()
- 求每门课的平均分,最高分,最低分。
- spark.sql("SELECT course,avg(score),max(score),min(score) from scs group by course").show()
- 求每门课的选修人数及平均分,精确到2位小数。
-
spark.sql("SELECT course,count(course),round(avg(score),2) from scs group by course").show()
-
- 每门课的不及格人数,通过率(未成功)
-
spark.sql("SELECT a.nopass,(b.total-a.nopass)/b.total from (SELECT course,distinct(count(*))as nopass from scs where score<60 group by course)as a left join (SELECT course,distinct(count(course))as total from scs group by course)as b on a.course=b.course").show()
-
spark.sql("SELECT course, count(name) as n, avg(score) as avg FROM scs group by course").createOrReplaceTempView("a")
spark.sql("SELECT course, count(score) as notPass FROM scs WHERE score<60 group by course").createOrReplaceTempView("b")
spark.sql("SELECT * FROM a left join b on a.course=b.course").show()
spark.sql("SELECT a.course, round(a.avg, 2), b.notPass, round((a.n-b.notPass)/a.n, 2) as passRat FROM a left join b on a.course=b.course").show()
-