时间序列聚合:在Scala / Spark数据帧中展平“窗口”结构类型

时间:2018-08-24 13:19:39

标签: scala apache-spark dataframe

为了汇总一个时间表(例如每10分钟),我使用了“ groupBy”和“ window”,如下所示:

val df2 = df.groupBy(
  window($"timestamp", "10 minutes"))
  .avg("field")

df2.show()看起来像

+-------------------------------------------+----------+
|                                     window|avg(field)|
+-------------------------------------------+----------+
| [2018-06-10 03:30:00, 2018-06-10 03:40:00]|22        |
| [2018-06-10 03:30:00, 2018-06-10 03:40:00]|42        |
| [2018-06-10 03:30:00, 2018-06-10 03:40:00]|60        | 
+-------------------------------------------+----------+

这是其架构:

root
 |-- window: struct (nullable = true)
 |    |-- start: timestamp (nullable = true)
 |    |-- end: timestamp (nullable = true)
 |-- avg(field): int (nullable = true)

我想将其保存到csv中,但我做不到:

CSV data source does not support struct<start:timestamp,end:timestamp>

您知道如何展平窗口列吗?还是有更好的方法来汇总这样的时间序列?

非常感谢

0 个答案:

没有答案
相关问题