2
有沒有辦法將納秒級的時間戳值轉換爲火花中的時間戳。我從csv文件獲得輸入,並且timstamp值的格式爲 12-12-2015 14:09:36.992415+01:00
。這是我試過的代碼。將納秒級的字符串轉換爲火花中的時間戳
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)
輸出是
+---+-----------------------+---------------------+
|ID |TIMESTAMP_VALUE |TIMESTAMP_CONV |
+---+-----------------------+---------------------+
|1 |12-12-2015 14:09:36.992|null |
+---+-----------------------+---------------------+
我能時間戳轉換使用格式MM-dd-yyyy HH:mm:ss.SSS
毫秒。麻煩與納秒和時區格式。
輸出在TIMESTAMP_CONV列上的一個更正爲空,這意味着轉換失敗。 – Abin
您可以嘗試提供自定義架構爲CSV列值作爲時間戳 –
我試圖使用自定義架構 ' import org.apache.spark.sql.types._; VAL customSchema = StructType( SEQ(StructField( 「ID」,DataTypes.IntegerType,真), StructField( 「TIMESTAMP_VALUE」,DataTypes.TimestampType,真) ) ); ' 但現在我得到錯誤_java.lang.ClassCastException:scala.Tuple2不能轉換爲java.lang.Integer_ – Abin