目录
问题表象
今日遇到Distinct window functions are not supported
AnalysisException: u'Distinct window functions are not supported: count(distinct color#1926)
恭喜你踩到了一个坑
你竟然想在窗口函数应用count distinct,可见得你对开窗函数的喜爱
但很遗憾窗口函数并不是万能,就像你看到的,比如窗口函数不支持count distinct
窗口函数不支持count distinct怎么办,需要变通
请记住:窗口函数都可以用普通的sql逻辑解决。
『近似』解决办法
#approx_count_distinct supports a window
df = df.withColumn('distinct_color_count_over_the_last_week', F.approx_count_distinct("color").over(w))
#approx_count_distinct 从 pyspark 2.1 开始这个函数可以帮你实现近似的计算。