casting a column dynamically in spark dataframe

后端 未结 2 976
半阙折子戏
半阙折子戏 2021-01-24 07:10

I want to to be able to create a new column out of an existing column(of type string) and cast it to a type dynamically.

resultDF = resultDF.withColumn(newColumn         


        
2条回答
  •  忘了有多久
    2021-01-24 07:33

    Why not use string descriptions?

    scala> col("foo").cast("int")
    res2: org.apache.spark.sql.Column = CAST(foo AS INT)
    
    scala> col("foo").cast("string")
    res3: org.apache.spark.sql.Column = CAST(foo AS STRING)
    

    Otherwise use DataType, which will cover all primitive types and basic collections.

提交回复
热议问题