Distinct window functions are not supported: count(distinct _w0# ) windowspecdefinition

spark | 2021-05-14 17:24:28

spark 开窗 使用 countDistinct异常

Distinct window functions are not supported: count(distinct _w0# ) windowspecdefinition

groupby 可以直接使用countDistinct,但是 over(Window.partionBy) 不可以

方案一、可以使用approx_count_distinct,但是它是概率统计,非精确统计,其是使用HyperLogLog实现的

import org.apache.spark.sql.functions._

val windowSpec = Window.partitionBy($"site_id",$"pxSessionId").orderBy($"timeMs").rowsBetween(Long.MinValue,Long.MaxValue)

res.withColumn("pageViewNumber",approx_count_distinct(when($"event_name" === "pageview",$"pxSessionId")).over(windowSpec))

 

方案二(推荐)、collect_set与size结合使用,示例如下

import org.apache.spark.sql.functions._

val windowSpec = Window.partitionBy($"site_id",$"pxSessionId").orderBy($"timeMs").rowsBetween(Long.MinValue,Long.MaxValue)

res.withColumn("pageViewNumber",size(collect_set(when($"event_name" === "pageview",$"pxSessionId")).over(windowSpec)))

 

 

 

 

 

 

登录后即可回复 登录 | 注册
    
关注编程学问公众号