How to convert a string column with milliseconds to a timestamp with milliseconds in Spark 2.1 using Scala?

后端 未结 3 594
日久生厌
日久生厌 2020-12-06 06:42

I am using Spark 2.1 with Scala.

How to convert a string column with milliseconds to a timestamp with milliseconds?

I tried the following code from the quest

3条回答
  •  我在风中等你
    2020-12-06 07:09

    There is an easier way than making a UDF. Just parse the millisecond data and add it to the unix timestamp (the following code works with pyspark and should be very close the scala equivalent):

    timeFmt = "yyyy/MM/dd HH:mm:ss.SSS"
    df = df.withColumn('ux_t', unix_timestamp(df.t, format=timeFmt) + substring(df.t, -3, 3).cast('float')/1000)
    

    Result: '2017/03/05 14:02:41.865' is converted to 1488722561.865

提交回复
热议问题