Spark SQL 将字符串转换为时间戳 [英] Spark SQL converting string to timestamp

查看:391
本文介绍了Spark SQL 将字符串转换为时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Spark SQL 的新手,正在尝试将字符串转换为 Spark 数据框中的时间戳.我在名为 time_string

I'm new to Spark SQL and am trying to convert a string to a timestamp in a spark data frame. I have a string that looks like '2017-08-01T02:26:59.000Z' in a column called time_string

我将此字符串转换为时间戳的代码是

My code to convert this string to timestamp is

CAST (time_string AS Timestamp)

但这给了我 2017-07-31 19:26:59

为什么它会改变时间?有没有办法在不改变时间的情况下做到这一点?

Why is it changing the time? Is there a way to do this without changing the time?

感谢您的帮助!

推荐答案

您可以使用 unix_timestamp 函数将 utc 格式的日期转换为时间戳

You could use unix_timestamp function to convert the utc formatted date to timestamp

val df2 = Seq(("a3fac", "2017-08-01T02:26:59.000Z")).toDF("id", "eventTime")

df2.withColumn("eventTime1", unix_timestamp($"eventTime", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").cast(TimestampType))

输出:

+-------------+---------------------+
|userid       |eventTime            |
+-------------+---------------------+
|a3fac        |2017-08-01 02:26:59.0|
+-------------+---------------------+

希望这会有所帮助!

这篇关于Spark SQL 将字符串转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆