我有一个像这样的数据框
df.show(5)
kv |list1 |list2 |p
[k1,v2|[1,2,5,9 |[5,1,7,9,6,3,1,4,9] |0.5
[k1,v3|[1,2,5,8,9|[5,1,7,9,6,3,1,4,15] |0.9
[k2,v2|[77,2,5,9]|[0,1,8,9,7,3,1,4,100]|0.01
[k5,v5|[1,0,5,9 |[5,1,7,9,6,3,1,4,3] |0.3
[k9,v2|[1,2,5,9 |[5,1,7,9,6,3,1,4,200]|2.5
df.count()
5200158
我想获得最大p的行,这对我有用,但我不知道是否有另一种清洁方式
val f = df.select(max(struct(
col("pp") +: df.columns.collect { case x if x != "p" => col(x) }: _*
))).first()
答案 0 :(得分:6)
只需按顺序排序:
$ ng new my-new-app
或
import org.apache.spark.sql.functions.desc
df.orderBy(desc("pp")).take(1)
答案 1 :(得分:3)
您也可以使用Window-Functions,如果选择行的逻辑变得更复杂(全局最小/最大值除外),这尤其有用:
import org.apache.spark.sql.expressions.Window
df
.withColumn("max_p",max($"p").over(Window.partitionBy()))
.where($"p" === $"max_p")
.drop($"max_p")
.first()