Mat*_*lar 3 apache-spark apache-spark-sql pyspark
这是我当前的数据集:
from pyspark.sql import Window
import pyspark.sql.functions as psf
df = spark.createDataFrame([("2","1",1),
("3","2",2)],
schema = StructType([StructField("Data", StringType()),
StructField("Source",StringType()),
StructField("Date", IntegerType())]))
display(df.withColumn("Result",psf.collect_set("Data").over(Window.partitionBy("Source").orderBy("Date"))))
Run Code Online (Sandbox Code Playgroud)
输出:
数据 | 来源 | 日期 | 结果 |
---|---|---|---|
2 | 1 | 1 | [“2”] |
3 | 1 | 2 | [“2”,“3”] |
为什么在窗口上使用collect_set函数时,3
列的第一行中缺少值?Result
ordered
我也尝试过使用collect_list
,但得到了相同的结果。
我想要的输出是:
数据 | 来源 | 日期 | 结果 |
---|---|---|---|
2 | 1 | 1 | [“2”,“3”] |
3 | 1 | 2 | [“2”,“3”] |
其中值的顺序Result
被保留 - 第一个是 where Date = 1
,第二个是Date = 2
unboundedPreceding
您需要使用带有和 的窗口Window.unboundedFollowing
:
Window.partitionBy("Source").orderBy("Date") \
.rowsBetween(Window.unboundedPreceding, Window.unboundedFollowing)
Run Code Online (Sandbox Code Playgroud)
rowsBetween(Window.unboundedPreceding, Window.currentRow)
默认情况下,当您有 Spark 时,Spark 使用orderBy
归档时间: |
|
查看次数: |
226 次 |
最近记录: |