Spark Scala 有条件地添加到 agg
Spark Scala Conditionally add to agg
是否可以在 Spark Scala 中有条件地添加聚合?
我想通过有条件地添加 collect_set
来干掉以下代码
示例:
val aggDf = if (addId) groups.agg(
count(lit(1)).as("Count"),
percentile_approx($"waitTime",lit(0.5), lit(10000)),
collect_set("Id").as("Ids")
)
else groups.agg(
count(lit(1)).as("Count"),
percentile_approx($"waitTime",lit(0.5), lit(10000))
)
也许这是编写整个代码的更好方法。
谢谢。
您可以按顺序存储聚合列并根据需要更改顺序:
var aggCols = Seq(count(lit(1)).as("Count"),
percentile_approx($"waitTime",lit(0.5), lit(10000)))
if(addId) aggCols = aggCols :+ collect_set("Id").as("Ids")
val aggDf = groups.agg(aggCols.head, aggCols.tail:_*)
是否可以在 Spark Scala 中有条件地添加聚合?
我想通过有条件地添加 collect_set
示例:
val aggDf = if (addId) groups.agg(
count(lit(1)).as("Count"),
percentile_approx($"waitTime",lit(0.5), lit(10000)),
collect_set("Id").as("Ids")
)
else groups.agg(
count(lit(1)).as("Count"),
percentile_approx($"waitTime",lit(0.5), lit(10000))
)
也许这是编写整个代码的更好方法。
谢谢。
您可以按顺序存储聚合列并根据需要更改顺序:
var aggCols = Seq(count(lit(1)).as("Count"),
percentile_approx($"waitTime",lit(0.5), lit(10000)))
if(addId) aggCols = aggCols :+ collect_set("Id").as("Ids")
val aggDf = groups.agg(aggCols.head, aggCols.tail:_*)