我想计算给定unix时间戳记的ArrayType列的滚动总和,并以2秒为增量对其进行分组。示例输入/输出如下。我认为Window()函数会起作用,对于PySpark来说我还很陌生,并且完全迷失了。任何输入,不胜感激!
输入:
timestamp vars
2 [1,2,1,2]
2 [1,2,1,2]
3 [1,1,1,2]
4 [1,3,4,2]
5 [1,1,1,3]
6 [1,2,3,5]
9 [1,2,3,5]
预期输出:
+---------+-----------------------+
|timestamp|vars |
+---------+-----------------------+
|2 |[2.0, 4.0, 2.0, 4.0] |
|4 |[4.0, 8.0, 7.0, 8.0] |
|6 |[6.0, 11.0, 11.0, 16.0]|
|10 |[7.0, 13.0, 14.0, 21.0]|
+---------+-----------------------+
谢谢!
编辑:多个列可以具有相同的时间戳记/它们可能不是连续的。 var的长度也可能大于3。请寻找稍微通用的解决方案。
答案 0 :(得分:2)
对于Spark 2.4+,您可以使用数组函数和高阶函数。此解决方案适用于不同的数组大小(如果每行之间不同,则会发生事件)。以下是解释的步骤:
首先,分组2秒,然后将vars
收集到数组列中:
df = df.groupBy((ceil(col("timestamp") / 2) * 2).alias("timestamp")) \
.agg(collect_list(col("vars")).alias("vars"))
df.show()
#+---------+----------------------+
#|timestamp|vars |
#+---------+----------------------+
#|6 |[[1, 1, 1], [1, 2, 3]]|
#|2 |[[1, 1, 1], [1, 2, 1]]|
#|4 |[[1, 1, 1], [1, 3, 4]]|
#+---------+----------------------+
在这里,我们将每个连续2秒进行分组,并将vars
数组收集到一个新列表中。
现在,使用Window规范,您可以收集累积值并使用flatten
来展平子数组:
w = Window.orderBy("timestamp").rowsBetween(Window.unboundedPreceding, Window.currentRow)
df = df.withColumn("vars", flatten(collect_list(col("vars")).over(w)))
df.show()
#+---------+------------------------------------------------------------------+
#|timestamp|vars |
#+---------+------------------------------------------------------------------+
#|2 |[[1, 1, 1], [1, 2, 1]] |
#|4 |[[1, 1, 1], [1, 2, 1], [1, 1, 1], [1, 3, 4]] |
#|6 |[[1, 1, 1], [1, 2, 1], [1, 1, 1], [1, 3, 4], [1, 1, 1], [1, 2, 3]]|
#+---------+------------------------------------------------------------------+
最后,将aggregate
函数与zip_with
结合使用以对数组求和:
t = "aggregate(vars, cast(array() as array<double>), (acc, a) -> zip_with(acc, a, (x, y) -> coalesce(x, 0) + coalesce(y, 0)))"
df.withColumn("vars", expr(t)).show(truncate=False)
#+---------+-----------------+
#|timestamp|vars |
#+---------+-----------------+
#|2 |[2.0, 3.0, 2.0] |
#|4 |[4.0, 7.0, 7.0] |
#|6 |[6.0, 10.0, 11.0]|
#+---------+-----------------+
放在一起:
from pyspark.sql.functions import ceil, col, collect_list, flatten, expr
from pyspark.sql import Window
w = Window.orderBy("timestamp").rowsBetween(Window.unboundedPreceding, Window.currentRow)
t = "aggregate(vars, cast(array() as array<double>), (acc, a) -> zip_with(acc, a, (x, y) -> coalesce(x, 0) + coalesce(y, 0)))"
nb_seconds = 2
df.groupBy((ceil(col("timestamp") / nb_seconds) * nb_seconds).alias("timestamp")) \
.agg(collect_list(col("vars")).alias("vars")) \
.withColumn("vars", flatten(collect_list(col("vars")).over(w))) \
.withColumn("vars", expr(t)).show(truncate=False)
答案 1 :(得分:1)
使用sum
窗口函数计算运行总和,并使用row_number
每隔第二个时间戳记行一次。
from pyspark.sql import Window
w = Window.orderBy(col('timestamp'))
result = df.withColumn('summed_vars',array([sum(col('vars')[i]).over(w) for i in range(3)])) #change the value 3 as desired
result.filter(col('rnum')%2 == 0).select('timestamp','summed_vars').show()
根据您的时间间隔更改%2
。
编辑:使用window
按时间间隔分组。假设timestamp
列的数据类型为timestamp
。
from pyspark.sql import Window
from pyspark.sql.functions import window,sum,row_number,array,col
w = Window.orderBy(col('timestamp'))
result = df.withColumn('timestamp_interval',window(col('timestamp'),'2 second')) \
.withColumn('summed_vars',array(*[sum(col('vars')[i]).over(w) for i in range(4)]))
w1 = Window.partitionBy(col('timestamp_interval')).orderBy(col('timestamp').desc())
final_result = result.withColumn('rnum',row_number().over(w1))
final_result.filter(col('rnum')==1).drop(*['rnum','vars']).show()