如何在 Spark SQL 中格式化日期? [英] How to format date in Spark SQL?

查看:57
本文介绍了如何在 Spark SQL 中格式化日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将这个给定的日期格式:2019-10-22 00:00:00 转换为这个:2019-10-22T00:00:00.000Z

I need to transform this given date format: 2019-10-22 00:00:00 to this one: 2019-10-22T00:00:00.000Z

我知道这可以通过以下方式在某些数据库中完成:

I know this could be done in some DB via:

在 AWS Redshift 中,您可以使用以下方法实现:

In AWS Redshift, you can achieve this using the following:

TO_DATE('{RUN_DATE_YYYY/MM/DD}', 'YYYY/MM/DD') || 'T00:00:00.000Z' AS VERSION_TIME

但我的平台是 Spark SQL,所以以上两个都不适合我,我能得到的最好的就是使用这个:

But my platform is Spark SQL, so neither above two work for me, the best I could get is using this:

concat(d2.VERSION_TIME, 'T00:00:00.000Z') as VERSION_TIME

这有点hacky,但仍然不完全正确,有了这个,我得到了这个日期格式:2019-10-25 00:00:00T00:00:00.000Z,但是字符串中间的这部分 00:00:00 是多余的,我不能把它留在那里.

which is a bit hacky, but still not completely correct, with this, I got this date format: 2019-10-25 00:00:00T00:00:00.000Z, but this part 00:00:00 in the middle of the string is redundant and I cannot leave it there.

任何有任何见解的人都将不胜感激!

Anyone has any insight here would be greatly appreciated!

推荐答案

这是我认为的自然方式.

This is the natural way I think.

spark.sql("""SELECT date_format(to_timestamp("2019-10-22 00:00:00", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") as date""").show(false)

结果是:

+------------------------+
|date                    |
+------------------------+
|2019-10-22T00:00:00.000Z|
+------------------------+

这篇关于如何在 Spark SQL 中格式化日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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