Spark SQL converting string to timestamp

前端 未结 2 2021
梦谈多话
梦谈多话 2020-12-31 02:36

I\'m new to Spark SQL and am trying to convert a string to a timestamp in a spark data frame. I have a string that looks like \'2017-08-01T02:26:59.000Z\' in a

2条回答
  •  星月不相逢
    2020-12-31 03:31

    You could use unix_timestamp function to convert the utc formatted date to timestamp

    val df2 = Seq(("a3fac", "2017-08-01T02:26:59.000Z")).toDF("id", "eventTime")
    
    df2.withColumn("eventTime1", unix_timestamp($"eventTime", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").cast(TimestampType))
    

    Output:

    +-------------+---------------------+
    |userid       |eventTime            |
    +-------------+---------------------+
    |a3fac        |2017-08-01 02:26:59.0|
    +-------------+---------------------+
    

    Hope this helps!

提交回复
热议问题