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

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

问题描述

我有一个带有以下格式的日期的sparksql日期框架:"26MAR2015".在以下问题中,他们将 to_date 函数与java simpledataformat一起使用:将pyspark字符串转换为日期格式.

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天全站免登陆