【问题标题】:Spark read dataframe column value as string [duplicate]Spark将数据框列值读取为字符串[重复]
【发布时间】:2018-09-15 01:12:57
【问题描述】:

我在 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 以附加我的自定义字符串?

【问题讨论】:

    标签: scala apache-spark apache-spark-sql apache-spark-2.0


    【解决方案1】:

    litconcat 可以解决问题。您可以使用lit 函数给出字符串值,使用concat 函数可以将其连接到列的字符串值。

    import org.apache.spark.sql.functions._
    
    df.withColumn("col1", when(col("col1").isNull,
      concat(col("col2"), lit("some_string"))))
    

    【讨论】:

      【解决方案2】:

      您可以使用lit 函数将字符串值更改为Column 并使用concat 函数。

      val df1 = df.withColumn("col1" ,
            when( col("col1").isNull , concat(col("col2"), lit("some_string")))
      

      希望这会有所帮助! )

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-14
        • 2016-10-11
        • 2019-09-14
        • 2018-02-26
        • 2018-10-18
        • 1970-01-01
        • 2016-12-25
        • 1970-01-01
        相关资源
        最近更新 更多