我刚尝试在窗口上执行countDistinct并出现此错误:
AnalysisException: u'Distinct window functions are not supported: count(distinct color#1926)
Run Code Online (Sandbox Code Playgroud)
有没有办法在pyspark中对窗口进行明确计数?
这是一些示例代码:
from pyspark.sql.window import Window
from pyspark.sql import functions as F
#function to calculate number of seconds from number of days
days = lambda i: i * 86400
df = spark.createDataFrame([(17, "2017-03-10T15:27:18+00:00", "orange"),
(13, "2017-03-15T12:27:18+00:00", "red"),
(25, "2017-03-18T11:27:18+00:00", "red")],
["dollars", "timestampGMT", "color"])
df = df.withColumn('timestampGMT', df.timestampGMT.cast('timestamp'))
#create window by casting timestamp to long (number of seconds)
w = (Window.orderBy(F.col("timestampGMT").cast('long')).rangeBetween(-days(7), 0))
df = df.withColumn('distinct_color_count_over_the_last_week', F.countDistinct("color").over(w))
df.show()
Run Code Online (Sandbox Code Playgroud)
这是我想看到的输出:
+-------+--------------------+------+---------------------------------------+
|dollars| timestampGMT| …Run Code Online (Sandbox Code Playgroud)