我在Spark 2.2中有数据框,我想将列值读为字符串。
val df1 = df.withColumn("col1" ,
when( col("col1").isNull , col("col2") +"some_string" )
当col1为null时,我想在col2中获取字符串值并在那里附加我的逻辑。
问题是我总是col("col2")
为org.apache.spark.sql.Column
。如何将此值转换为String
以附加我的自定义字符串?
答案 0 :(得分:2)
lit
和concat
可以解决问题。您可以使用lit
函数给出和字符串值,并使用concat
函数将它连接到列的字符串值。
import org.apache.spark.sql.functions._
df.withColumn("col1", when(col("col1").isNull,
concat(col("col2"), lit("some_string"))))
答案 1 :(得分:1)
您可以使用lit
函数将字符串值更改为Column
并使用concat
函数。
val df1 = df.withColumn("col1" ,
when( col("col1").isNull , concat(col("col2"), lit("some_string")))
希望这有帮助! )