转:sparkRDD练习题

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41290471/article/details/83685335
给定数据如下:
班级ID 姓名 年龄 性别 科目 成绩
12 张三 25 男 chinese 50
12 张三 25 男 math 60
12 张三 25 男 english 70
12 李四 20 男 chinese 50
12 李四 20 男 math 50
12 李四 20 男 english 50
12 王芳 19 女 chinese 70
12 王芳 19 女 math 70
12 王芳 19 女 english 70
13 张大三 25 男 chinese 60
13 张大三 25 男 math 60
13 张大三 25 男 english 70
13 李大四 20 男 chinese 50
13 李大四 20 男 math 60
13 李大四 20 男 english 50
13 王小芳 19 女 chinese 70
13 王小芳 19 女 math 80
13 王小芳 19 女 english 70

需求如下:
1. 一共有多少人参加考试?
1.1 一共有多少个小于20岁的人参加考试?
1.2 一共有多少个等于20岁的人参加考试?
1.3 一共有多少个大于20岁的人参加考试?

2. 一共有多个男生参加考试?
2.1 一共有多少个女生参加考试?

3. 12班有多少人参加考试?
3.1 13班有多少人参加考试?

4. 语文科目的平均成绩是多少?
4.1 数学科目的平均成绩是多少?
4.2 英语科目的平均成绩是多少?

5. 单个人平均成绩是多少?

6. 12班平均成绩是多少?
6.1 12班男生平均总成绩是多少?
6.2 12班女生平均总成绩是多少?
6.3 同理求13班相关成绩

7. 全校语文成绩最高分是多少?
7.1 12班语文成绩最低分是多少?
7.2 13班数学最高成绩是多少?

8. 总成绩大于150分的12班的女生有几个?

import org.apache.spark.{SparkConf, SparkContext}
 
object RDDtest {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setAppName("RDDtest")
      .setMaster("local")
    val sc = new SparkContext(conf)
 
    val file = sc.textFile("F:\\Spark\\test.txt")
 
    //    val tuple111=file.map(_.split(" ")).map(x=>(x(1),x(2)))
    val tupled = file.map(line => {
      val fields = line.split(" ")
      val classid = fields(0)
      val name = fields(1)
      val age = fields(2)
      val sex = fields(3)
      val course = fields(4)
      val score = fields(5)
      (classid, name, age, sex, course, score)
 
    })
    //测试tupled
    //    println(tupled.collect().toBuffer)
    //多少人参加了考试
    //     val nums=tupled.map(_._2).distinct().count()
    //     println(nums)
    //    1.1 一共有多少个小于20岁的人参加考试?
    //    val nums =tupled.map(x =>(x._2,x._3)).distinct().filter(_._2.toInt<20).count()
    //    println(nums)
    //    1.2 一共有多少个等于20岁的人参加考试?
    //    1.3 一共有多少个大于20岁的人参加考试?
    //
    //    2. 一共有多个男生参加考试?
    //    val nums = tupled.map(x=>(x._2,x._4)).distinct().filter(_._2 =="男").count()
    //    println(nums)
    //    2.1 一共有多少个女生参加考试?
    //
    //    3. 12班有多少人参加考试?
    //val nums = tupled.map(x=>(x._1,x._2)).distinct().filter(_._1 ==12).count()
    //    println(nums)
    //    3.1 13班有多少人参加考试?
    //
    //    4. 语文科目的平均成绩是多少?
    //    val nums = tupled.map(x=>(x._5,x._6,1))         有错
    //  .filter(_._1=="chinese").map(x=>(x._2,x._3))  有错
    //    .map(x=>(x._2.toDouble+x._2.toDouble,x._3.toInt+x._3.toInt)) 有错
 
    //    val nums = tupled.map(x=>(x._5,x._6.toDouble)).filter(_._1 =="chinese").reduceByKey(_+_)
    //    val num2 =tupled.filter(_._5=="chinese").flatMap(_._6).mean()
    //    val num3 =tupled.filter(x=>x._5.contains("")||x._5.contains("")).flatMap(_._6).mean()
    //    println(num2)
 
    //    4.1 数学科目的平均成绩是多少?
    //    4.2 英语科目的平均成绩是多少?
    //
    //    5. 单个人平均成绩是多少?
 
    //    val step1 = tupled.map(x => (x._2, x._6.toDouble)).groupByKey().mapValues(_.toList)
    //    val step2 = step1.mapValues(x => x.sum / x.size)
    //    step1.foreach(println)
    //    step2.foreach(println)
 
 
    //    6. 12班平均成绩是多少?
    //val step1 = tupled.filter(_._1=="12").map(x=>(x._1,x._6.toDouble))
    //  .groupByKey().mapValues(x=>x.sum/x.size)
    //    step1.foreach(println)
 
    //    6.1 12班男生平均总成绩是多少?
    //    6.2 12班女生平均总成绩是多少?
    //    6.3 同理求13班相关成绩
    //
    //    7. 全校语文成绩最高分是多少?
 
    //top返回最大的几个元素不需要排序了
    //    val step1 = tupled.filter(_._5=="chinese").map(x=>(x._2,x._6)).sortBy(_._2,false).take(1)
    //step1.foreach(println)
    //    7.1 12班语文成绩最低分是多少?
    //    7.2 13班数学最高成绩是多少?
    //
    //    8. 总成绩大于150分的12班的女生有几个?
    //    val step1 = tupled.filter(_._1 == "12").map(x => (x._2, x._6.toDouble))
    //      .groupByKey().mapValues(_.sum).filter(_._2>150).count()
    //    println(step1)
    sc.stop
}}
————————————————
版权声明:本文为CSDN博主「Ethan130」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41290471/article/details/83685335

发布了12 篇原创文章 · 获赞 130 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/wdr2003/article/details/102599647