学科、姓名、分数。 分别计算每门学科分数最高的前三名,并显示名次。写入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()
  }

}

你可能感兴趣的:(学科、姓名、分数。 分别计算每门学科分数最高的前三名,并显示名次。写入MySQL中。)