我知道如何在Spark SQL中编写UDF:
def belowThreshold(power: Int): Boolean = {
return power < -40
}
sqlContext.udf.register("belowThreshold", belowThreshold _)
Run Code Online (Sandbox Code Playgroud)
我可以做类似的定义聚合函数吗?这是怎么做到的?
对于上下文,我想运行以下SQL查询:
val aggDF = sqlContext.sql("""SELECT span, belowThreshold(opticalReceivePower), timestamp
FROM ifDF
WHERE opticalReceivePower IS NOT null
GROUP BY span, timestamp
ORDER BY span""")
Run Code Online (Sandbox Code Playgroud)
它应该返回类似的东西
Row(span1, false, T0)
我希望聚合函数告诉我opticalReceivePower在定义的组中是否有任何值span,timestamp哪些值低于阈值.我是否需要以不同的方式将UDAF写入上面粘贴的UDF?
scala aggregate-functions user-defined-functions apache-spark apache-spark-sql
我有一个包含两列数据的数据框,如下所示
+----+-----------------+
|acct| device|
+----+-----------------+
| B| List(3, 4)|
| C| List(3, 5)|
| A| List(2, 6)|
| B|List(3, 11, 4, 9)|
| C| List(5, 6)|
| A|List(2, 10, 7, 6)|
+----+-----------------+
Run Code Online (Sandbox Code Playgroud)
我需要如下结果
+----+-----------------+
|acct| device|
+----+-----------------+
| B|List(3, 4, 11, 9)|
| C| List(3, 5, 6)|
| A|List(2, 6, 7, 10)|
+----+-----------------+
Run Code Online (Sandbox Code Playgroud)
我试过如下,但似乎不起作用
df.groupBy("acct").agg(concat("device"))
df.groupBy("acct").agg(collect_set("device"))
请让我知道如何使用 Scala 实现这一目标?