使用 Spark SQL 从 ISO 8601 解析日期时间 [英] Parsing datetime from ISO 8601 using Spark SQL

查看:28
本文介绍了使用 Spark SQL 从 ISO 8601 解析日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要做这个但反过来.

我的date是这种格式YYYY-MM-DDThh:mm:ss,我想要两列YYYY-MM-DDhh:mm ,如果我愿意,我可以连接某些查询.

My dates are in this format YYYY-MM-DDThh:mm:ss, I want two columns YYYY-MM-DD and hh:mm that I can concat, if I want to, for certain queries.

我在使用 convert() 时出错;我认为目前 Spark SQL 不支持此功能.

I get an error when using convert(); I assume this is not supported currently with Spark SQL.

当我使用 date(datetime)timestamp(datetime) 时,我得到所有返回的空值.但是,minute(datetime)hour(datetime) 有效.

When I use date(datetime) or timestamp(datetime), I get all null values returned. However, minute(datetime) and hour(datetime) work.

目前,使用这个

concat(date,' ', hour,':', (case when minute < 10 then concat('0',minute) else minute end)) as DateTime
from (select OtherDateOnlyColumn as date, minute(datetime) as minute, hour(datetime) as hour from ...)

这显然效率不高.

推荐答案

我刚刚在这个查询上尝试使用 date() 并且它有效:

I just tried with date() on this query and it works:

select date(datetime) from df

也许你表中的日期是字符串类型;您应该使用

Maybe the date in your table is string type; you should check the data types of the columns with

DESCRIBE your_table

如果日期是字符串类型,您可以使用 cast(datetime as timestamp) as newTimestamp,它在 Spark SQL 将日期时间转换回时间戳类型和从那里使用 date_format(newTimestamp, 'YYYY-MM-dd hh:mm') 的变体.

If the date is string type, you can use cast(datetime as timestamp) as newTimestamp which is available in Spark SQL to convert the datetime back to a timestamp type and use variants of date_format(newTimestamp, 'YYYY-MM-dd hh:mm') from there.

这篇关于使用 Spark SQL 从 ISO 8601 解析日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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