df1.createOrReplaceTempView("people")
spark.sql("select age from people where age is not null group by age").show
2、全局临时视图
df.createGlobalTempView("people")
spark.newSession().sql("SELECT * FROM global_temp.people").show()
全局临时视图可跨session使用,保存在数据库global_temp中
聚合函数
count_distinct 近似值 avg mean 平均值 collect_list 聚合指定字段的值到 list collect_set 聚合指定字段的值到 set countDistinct 去重计数 select count(distinct class) 去重计数 first 分组第一个元素 last 分组最后一个元素 grouping sets 等同于group by A,B union group by A,C max 最大值 min 最小值 sum 求和 sumDistinct select sum(distinct class) 非重复值求和
窗口函数在是聚合函数后面使用over()标记要使用窗口功能,填写partition by指明分区,使用order by指明排序,over函数 外面指明需要返回哪些值 rank() 排名,返回数据项在分组中的排名,排名相等会在名次中留下空位 1,2,2,4 dense_rank() 排名,返回数据项在分组中的排名,排名相等会在名次中不会留下空位 row_number(): 行号,为每条记录返回一个数字 1,2,3,4 spark.sql("select name,age rank() over(partition by name order by age) as rank from global_temp.Human").show