学科、姓名、分数。 分别计算每门学科分数最高的前三名,并显示名次。写入MySQL中。

hadoop,xiaoji,99
hadoop,mingyang,98
hadoop,susan,99
spark,xiaoli,98
spark,xiaohua,95
hive,lele,89
spark,zhangsan,99
hive,tim,97
hive,kebi,94
hive,mike,99
hadoop,xiaoming,96
hive,susan,96
spark,wenwen,97
hive,kimi,90

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}

object Need2 {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName(this.getClass.getSimpleName)
    val sc = new SparkContext(conf)
    val sQLContext = new SQLContext(sc)
    import sQLContext.implicits._
    val sourceFile: RDD[String] = sc.textFile("D:\\djz\\20200204\\teacherdata.txt")
    val df: DataFrame = sourceFile.map(line => {
      val split: Array[String] = line.split(",")
      (split(0), split(1), split(2).toInt)
    }).toDF("subject", "name", "score")
    df.createTempView("student")
    sQLContext.sql("select * from (select subject,name,score,row_number() over (partition by subject order by score desc) as sc" +
      " from student) t where sc<=3 ").show()
    sc.stop()
  }

}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值