【发布时间】:2017-12-06 19:02:25
【问题描述】:
我正在使用带有 Scala 的 Spark 2.1。
如何将以毫秒为单位的字符串列转换为以毫秒为单位的时间戳?
我从问题Better way to convert a string field into timestamp in Spark中尝试了以下代码
import org.apache.spark.sql.functions.unix_timestamp
val tdf = Seq((1L, "05/26/2016 01:01:01.601"), (2L, "#$@#@#")).toDF("id", "dts")
val tts = unix_timestamp($"dts", "MM/dd/yyyy HH:mm:ss.SSS").cast("timestamp")
tdf.withColumn("ts", tts).show(2, false)
但我得到的结果没有毫秒:
+---+-----------------------+---------------------+
|id |dts |ts |
+---+-----------------------+---------------------+
|1 |05/26/2016 01:01:01.601|2016-05-26 01:01:01.0|
|2 |#$@#@# |null |
+---+-----------------------+---------------------+
【问题讨论】:
标签: scala datetime apache-spark