开窗函数(javaAPI)
package SparkSQL.fun
import org.apache.spark.SparkConf
import org.apache.spark.sql.{Dataset, SparkSession}
object PartitionCode {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("func1").setMaster("local[*]")
val ss = SparkSession.builder().config(sparkConf).getOrCreate()
import ss.implicits._
val dataset:Dataset[(String,String,Int,String)] = ss.createDataset(Array(
("zs","c001",21,"男"),
("ls","c001",22,"女"),
("ww","c001",23,"男"),
("ml","c002",20,"女"),
("zb","c002",23,"男")
))
dataset.createTempView("student")
/**
* 查询表中的所有数据,并且每一个人后面跟上班级的平均年龄和总年龄
*("zs","c001",21,"男",20,200),
("ls","c001",22,"女",20,200),
("ww","c001",23,"男"),
("ml","c002",20,"女"),
("zb","c002",23,"男")
*/
val frame = ss.sql("select *, avg(_3) over(partition by _2) avg, sum(_3) over(partition by _2) sum from student")
frame.show()
println("-------------------------------")
val frame1 = ss.sql("select *, max(_3) over(partition by _2) max_age from student")
frame1.show()
ss.stop()
}
}
本文来自博客园,作者:jsqup,转载请注明原文链接:https://www.cnblogs.com/jsqup/p/16659616.html