Better way to convert a string field into timestamp in Spark

前端 未结 7 840
独厮守ぢ
独厮守ぢ 2020-11-27 16:29

I have a CSV in which a field is datetime in a specific format. I cannot import it directly in my Dataframe because it needs to be a timestamp. So I import it as string and

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-27 17:07

    Spark Version: 2.4.4

    scala> import org.apache.spark.sql.types.TimestampType
    import org.apache.spark.sql.types.TimestampType
    
    scala> val df = Seq("2019-04-01 08:28:00").toDF("ts")
    df: org.apache.spark.sql.DataFrame = [ts: string]
    
    scala> val df_mod = df.select($"ts".cast(TimestampType))
    df_mod: org.apache.spark.sql.DataFrame = [ts: timestamp]
    
    scala> df_mod.printSchema()
    root
     |-- ts: timestamp (nullable = true)
    

提交回复
热议问题