如何从select中获取多列?例如,我可以得到1列:
df1.alias("l")
.join(df2.alias("r"),Seq("id"))
.select(when($"l.score" > $"r.score", $"l.name").otherwise($"r.name"))
但是,假设我想获得超过1列(甚至是数据框中的所有属性),我不能做任何这些:
df1.alias("l")
.join(df2.alias("r"),Seq("id"))
.select(when($"l.score" > $"r.score", Seq($"l.id",$"l.name")).otherwise(Seq($"r.id",$"r.name")))
df1.alias("l")
.join(df2.alias("r"),Seq("id"))
.select(when($"l.score" > $"r.score", $"l.*").otherwise($"r.*"))
任何想法如何规避这个?
你可以返回struct
:
import org.apache.spark.sql.struct
when($"l.score" > $"r.score", struct($"l.name", $"l.score"))
.otherwise(struct($"r.name", "r.score"))
或适用于所有领域:
when($"l.score" > $"r.score", struct($"l.*"))
.otherwise(struct($"r.*"))