【发布时间】:2020-09-01 04:54:10
【问题描述】:
我必须在 spark Scala 中使用条件连接多个列,但它不适用于“if”。 我有以下数据框:
table:
+---+----+----+
| a| b| c|
+---+----+----+
| 0| 1| 1|
| 1| 0| 0|
| 1| 1| 0|
+---+----+----+
table.withColumn("concat", concat_ws(", ", (if($"a"===1){lit("D")} else{null}),
(if($"b"===1){lit("E")} else{null}),
(if($"c"===1){lit("F")} else{null})))
以下是最终要求的结果。
+---+----+----+------+
| a| b| c|concat|
+---+----+----+------+
| 0| 1| 1| E, F|
| 1| 0| 0| D|
| 1| 1| 0| D, E|
+---+----+----+------+
我不会像这样创建其他列:
val ftable = (table.withColumn("D", when ($"a"===1, lit("D")))
.withColumn("E", when ($"b"===1, lit("E")))
.withColumn("F", when ($"c"===1, lit("F"))))
val columnselection = ftable.select($"D", $"E" , $"F" )
val selection = columnselection.columns.map(col)
val animaliCol = ftable.select(ftable.col("*"), concat_ws(", ", selection : _*).as("concat"))
【问题讨论】:
标签: scala apache-spark apache-spark-sql