码迷,mamicode.com
首页 > 其他好文 > 详细

四率一直_sparkcore

时间:2020-05-05 17:49:28      阅读:54      评论:0      收藏:0      [点我收藏+]

标签:substr   substring   where   port   ext   rgs   style   函数   spl   

 1 package com.bawei.review01
 2 
 3 import org.apache.spark.rdd.RDD
 4 import org.apache.spark.sql.{DataFrame, SparkSession}
 5 
 6 case class StuScore(id:Int,clazz:String,score:Int)
 7 object SparkSqlZK {
 8 
 9   def main(args: Array[String]): Unit = {
10     val spark = SparkSession.builder().appName("data-from-file").master("local").getOrCreate()
15 
16     val lineRDD: RDD[String] = spark.sparkContext.textFile("./coredata/stu.txt")
17 
18     val stuscoreRdd: RDD[StuScore] = lineRDD.map(line => {
19       val strings: Array[String] = line.split(" ")
20       StuScore(strings(0).toInt, strings(1), strings(2).toInt)
21     })
22     //把RDD转换成DataFrame[StuScore]
23     import spark.implicits._
24     val stuscoreDF: DataFrame = stuscoreRdd.toDF()
25     stuscoreDF.createOrReplaceTempView("stuscore")
26 
27     spark.sql("select *,substring(clazz,0,2) jie from stuscore").createOrReplaceTempView("stuscore2")
28     //stuscore2
29     //id clazz score jie
30 
31     //开窗函数 row_number() over(partition by jie order by score desc) rk
32 
33     spark.sql("select * from " +
34            " (select *,row_number() over(partition by jie order by score desc) rk from stuscore2)" +
35            " where rk<=3").show()
36   }
37 }

 

四率一直_sparkcore

标签:substr   substring   where   port   ext   rgs   style   函数   spl   

原文地址:https://www.cnblogs.com/xjqi/p/12831446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!