将字符串转换为火花/蜂巢中的时间戳

发布于 2025-02-03 12:38:53 字数 483 浏览 2 评论 0原文

我需要将字符串转换为时间戳。

问题是输入来自 csv 文件,并包含日期时间值,例如:

3022年3022年3月3日上午

APR 27 2022 7:37 AM

如果我使用以下转换:

to_timestamp(to_timestamp(trim(datecolumn),'mmm dd yyyy h:mma'),'yyyy-mm-dd hh:mm:mm:ss')

它转换日期apr 27 2022 2022 7:37 AM正确>正确地> ,但是在转换3022年3月3日上午3:30 AM时引发错误,因为该月和日期值之间的额外空间,并且日期3不是03。

是否可以将这些2个字符串格式转换为DateTime?

I need to convert string to Timestamp.

The problem is that the input is coming from a csv file and contains date-time values such as:

Mar 3 2022 8:30AM

Apr 27 2022 7:37AM

If I use the following conversion:

to_timestamp(to_timestamp(trim(DateColumn), 'MMM dd yyyy h:mma'), 'yyyy-MM-dd HH:mm:ss')

It converts the date Apr 27 2022 7:37AM correctly, but throws error while converting Mar 3 2022 8:30AM because of the extra space between the Month and Date values and that the date 3 is not 03.

Is there a way to convert these 2 strings formats into Datetime?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不咬妳我踢妳 2025-02-10 12:38:53

建议您首先用单个空间均匀地替换多个空格,然后转换为时间戳。

val df1 = df.withColumn("ts", to_timestamp(regexp_replace(trim(col("ts")), "\\s+", " "), "MMM d y h:mma"))

It is recommended that you first uniformly replace multiple spaces with a single space, and then convert to timestamp.

val df1 = df.withColumn("ts", to_timestamp(regexp_replace(trim(col("ts")), "\\s+", " "), "MMM d y h:mma"))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文