【发布时间】:2017-07-02 23:42:13
【问题描述】:
如何在 Apache Spark 1.6 的 where 子句中使用聚合函数?
考虑以下DataFrame
+---+------+
| id|letter|
+---+------+
| 1| a|
| 2| b|
| 3| b|
+---+------+
如何选择letter 多次出现的所有行,即预期输出为
+---+------+
| id|letter|
+---+------+
| 2| b|
| 3| b|
+---+------+
这显然不起作用:
df.where(
df.groupBy($"letter").count()>1
)
我的示例是关于 count,但我也希望能够使用其他聚合函数(其结果)。
编辑:
只是为了计数,我只是想出了以下解决方案:
df.groupBy($"letter").agg(
collect_list($"id").as("ids")
)
.where(size($"ids") > 1)
.withColumn("id", explode($"ids"))
.drop($"ids")
【问题讨论】:
标签: scala apache-spark