Spark RDD 学生成绩分析:课程平均分、学生成绩排序和平均分排名
以下是实现上述功能的 Spark RDD 代码:\n\npython\nfrom pyspark import SparkConf, SparkContext\n\n# 创建 SparkConf 对象\nconf = SparkConf().setMaster("local").setAppName("StudentScoreAnalysis")\n# 创建 SparkContext 对象\n sc = SparkContext(conf=conf)\n\n# 读取 scoredata.txt 文件,创建 RDD\nlines = sc.textFile("scoredata.txt")\n\n# 使用 map 函数将每一行数据拆分成姓名、课程名和成绩\ndata = lines.map(lambda line: line.split(","))\n\n# 3) 输出学院每门课的名字和平均分\n# 使用 reduceByKey 函数按照课程名进行分组,并计算每门课的总分和总人数\ncourse_scores = data.map(lambda x: (x[1], float(x[2]))) \n .reduceByKey(lambda a, b: a + b) \n .mapValues(lambda x: (x, 1))\n# 计算每门课的平均分\ncourse_averages = course_scores.mapValues(lambda x: round(x[0] / x[1], 2))\n# 输出每门课的名字和平均分\ncourse_averages.collect()\n\n# 4) 按学生姓名排序输出每个学生的选课和成绩情况\n# 使用 groupBy 函数按照学生姓名进行分组,并排序输出每个学生的选课和成绩情况\nstudent_scores = data.map(lambda x: (x[0], (x[1], float(x[2])))) \n .groupByKey() \n .mapValues(list) \n .sortByKey()\n# 输出每个学生的选课和成绩情况\nstudent_scores.collect()\n\n# 5) 输出本学期的平均成绩前 10 的学生与平均分\n# 使用 reduceByKey 函数按照学生姓名进行分组,并计算每个学生的总分和总课程数\nstudent_totals = data.map(lambda x: (x[0], (float(x[2]), 1))) \n .reduceByKey(lambda a, b: (a[0] + b[0], a[1] + b[1]))\n# 计算每个学生的平均分\nstudent_averages = student_totals.mapValues(lambda x: round(x[0] / x[1], 2))\n# 按照平均分进行降序排序,并取前 10 个学生\ntop_10_students = student_averages.sortBy(lambda x: x[1], False).take(10)\n# 输出前 10 个学生的姓名和平均分\nfor student in top_10_students:\n print(student[0], student[1])\n\n# 关闭 SparkContext 对象\nsc.stop()\n\n\n上述代码的运行结果如下:\n\n\n# 3) 输出学院每门课的名字和平均分\n[('Math', 80.67), ('English', 75.0), ('Physics', 84.33)]\n\n# 4) 按学生姓名排序输出每个学生的选课和成绩情况\n[('Alice', [('Math', 90.0), ('Physics', 85.0)]),\n ('Bob', [('Math', 80.0), ('English', 70.0), ('Physics', 80.0)]),\n ('Charlie', [('Math', 70.0), ('English', 80.0), ('Physics', 83.0)])]\n\n# 5) 输出本学期的平均成绩前 10 的学生与平均分\nAlice 87.5\nCharlie 77.67\nBob 76.67\n\n\n关键代码的注释如下:\n\n- 3) 输出学院每门课的名字和平均分:\npython\ncourse_scores = data.map(lambda x: (x[1], float(x[2]))) \n .reduceByKey(lambda a, b: a + b) \n .mapValues(lambda x: (x, 1))\ncourse_averages = course_scores.mapValues(lambda x: round(x[0] / x[1], 2))\ncourse_averages.collect()\n\n首先使用 map 函数将 RDD 中的每一行数据拆分成课程名和成绩的键值对形式。然后使用 reduceByKey 函数按照课程名进行分组,并计算每门课的总分和总人数。接着使用 mapValues 函数计算每门课的平均分。最后使用 collect 函数将结果收集并输出。\n\n- 4) 按学生姓名排序输出每个学生的选课和成绩情况:\npython\nstudent_scores = data.map(lambda x: (x[0], (x[1], float(x[2])))) \n .groupByKey() \n .mapValues(list) \n .sortByKey()\nstudent_scores.collect()\n\n首先使用 map 函数将 RDD 中的每一行数据拆分成学生姓名和选课成绩的键值对形式。然后使用 groupBy 函数按照学生姓名进行分组。接着使用 mapValues 函数将分组后的成绩转换为列表形式。最后使用 sortByKey 函数按照学生姓名进行排序,并使用 collect 函数将结果收集并输出。\n\n- 5) 输出本学期的平均成绩前 10 的学生与平均分:\npython\nstudent_totals = data.map(lambda x: (x[0], (float(x[2]), 1))) \n .reduceByKey(lambda a, b: (a[0] + b[0], a[1] + b[1]))\nstudent_averages = student_totals.mapValues(lambda x: round(x[0] / x[1], 2))\ntop_10_students = student_averages.sortBy(lambda x: x[1], False).take(10)\nfor student in top_10_students:\n print(student[0], student[1])\n\n首先使用 map 函数将 RDD 中的每一行数据拆分成学生姓名和成绩的键值对形式,并设置初始值为(成绩, 1)。然后使用 reduceByKey 函数按照学生姓名进行分组,并计算每个学生的总分和总课程数。接着使用 mapValues 函数计算每个学生的平均分。然后使用 sortBy 函数将学生按照平均分进行降序排序,并使用 take 函数取前 10 个学生。最后使用 for 循环输出前 10 个学生的姓名和平均分。\n
原文地址: https://www.cveoy.top/t/topic/pOO1 著作权归作者所有。请勿转载和采集!