我得到了正确的输出
unix_timestamp()
val dataframe = Seq(
("2017-03-12 02:41:06"),
("2017-03-12 02:43:52"),
("2017-03-12 02:56:32"),
("2017-03-12 03:16:23"),
("2017-03-12 03:17:15"),
("2017-03-12 03:22:19"),
("2017-03-12 03:52:19"),
("2017-03-12 04:03:21")
).toDF("test_time")
dataframe.withColumn("convert_test", unix_timestamp($"test_time", "yyyy-MM-dd hh:mm:ss").cast("timestamp")).show()
输出:
+-------------------+--------------------+
| test_time| convert_test|
+-------------------+--------------------+
|2017-03-12 02:41:06|2017-03-12 02:41:...|
|2017-03-12 02:43:52|2017-03-12 02:43:...|
|2017-03-12 02:56:32|2017-03-12 02:56:...|
|2017-03-12 03:16:23|2017-03-12 03:16:...|
|2017-03-12 03:17:15|2017-03-12 03:17:...|
|2017-03-12 03:22:19|2017-03-12 03:22:...|
|2017-03-12 03:52:19|2017-03-12 03:52:...|
|2017-03-12 04:03:21|2017-03-12 04:03:...|
+-------------------+--------------------+
如果您的时区不同,则可以使用以下函数
from_utc_timestamp()
和
to_utc_timestamp()
转换时间戳。
希望这是有帮助的!