在sparksql中将日期从字符串转换为日期 [英] Converting date from string to date in sparksql

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

问题描述

我有一个 sparksql dateframe,日期格式如下:26MAR2015".在以下问题中,他们使用带有 java simpledataformat 的 to_date 函数:将日期从字符串转换为 Dataframes 中的日期格式 以转换日期中的字符串.我在以下问题中找不到有关此格式的更多信息:Convert pyspark string to date format.

I have a sparksql dateframe with dates in the following format: "26MAR2015". In following question they use the to_date function with java simpledataformat: Convert date from String to Date format in Dataframes to convert the strings in date. I could not find more information about this format in following question: Convert pyspark string to date format.

我没有找到适合我的案例的正确格式:

I do not find the correct format for my case:

spark.sql("""
SELECT TO_DATE(CAST(UNIX_TIMESTAMP('15MAR2015', '??????') AS TIMESTAMP)) AS newdate"""
).show()

推荐答案

您应该使用 ddMMMyyyy 作为格式字符串.

You should use ddMMMyyyy as the format string.

例如,您可以这样做:

spark.sql("""
SELECT CAST(FROM_UNIXTIME(UNIX_TIMESTAMP('15MAR2015', 'ddMMMyyyy')) AS date) AS newdate"""
).show()
#+----------+
#|   newdate|
#+----------+
#|2015-03-15|
#+----------+

您可以找到有关 Java SimpleDateFormat 的更多信息 这里.

You can find more information about the Java SimpleDateFormat here.

这篇关于在sparksql中将日期从字符串转换为日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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