Is there a way to convert a timestamp value with nano seconds to timestamp in spark. I get the input from a csv file and the timstamp value is of format
12-12-2015 14:09:36.992415+01:00
. This is the code I tried.
val date_raw_data = List((1, "12-12-2015 14:09:36.992415+01:00"))
val dateraw_df = sc.parallelize(date_raw_data).toDF("ID", "TIMESTAMP_VALUE")
val ts = unix_timestamp($"TIMESTAMP_VALUE", "MM-dd-yyyy HH:mm:ss.ffffffz").cast("double").cast("timestamp")
val date_df = dateraw_df.withColumn("TIMESTAMP_CONV", ts).show(false)
The output is
+---+-----------------------+---------------------+
|ID |TIMESTAMP_VALUE |TIMESTAMP_CONV |
+---+-----------------------+---------------------+
|1 |12-12-2015 14:09:36.992|null |
+---+-----------------------+---------------------+
I was able to convert a time stamp with millisecond using format MM-dd-yyyy HH:mm:ss.SSS
. Trouble is with nano second and timezone formats.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…