简体   繁体   English

尝试将字符串转换为 pyspark 数据帧中的 unix_timestamp 数据类型时出现 SparkUpgrade 异常

[英]Getting SparkUpgrade exception while trying to convert string to unix_timestamp datatype in pyspark dataframe

I am using spark3.1.1 and trying to convert string-datatype to unix_timestamp datatype using the below code.我正在使用 spark3.1.1 并尝试使用以下代码将字符串数据类型转换为 unix_timestamp 数据类型。

pattern = 'yyyy/MM/dd hh:mm:ss aa'
file_new = file1.withColumn('Incident_DateTime', unix_timestamp(file1['Incident_DateTime'], pattern).cast('timestamp'))  
file_new.select('Incident_DateTime').show(5, False)

Getting error on all actions -> select/display/show.在所有操作上出错 -> 选择/显示/显示。 PFB snapshot and help PFB 快照和帮助

org.apache.spark.SparkUpgradeException: You may get a different result due to the upgrading of Spark 3.0: Fail to recognize 'yyyy/MM/dd hh:mm:ss aa' pattern in the DateTimeFormatter. org.apache.spark.SparkUpgradeException:由于 Spark 3.0 的升级,您可能会得到不同的结果:无法识别 DateTimeFormatter 中的“yyyy/MM/dd hh:mm:ss aa”模式。 1) You can set spark.sql.legacy.timeParserPolicy to LEGACY to restore the behavior before Spark 3.0. 1) 您可以将 spark.sql.legacy.timeParserPolicy 设置为 LEGACY 以恢复 Spark 3.0 之前的行为。 2) You can form a valid datetime pattern with the guide from https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html 2)您可以使用https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html 中的指南形成有效的日期时间模式

From the link in your question:从您问题中的链接:

"am-pm: This outputs the am-pm-of-day. Pattern letter count must be 1.", "am-pm: 这将输出 am-pm-of-day。模式字母计数必须为 1。",

so the pattern should be:所以模式应该是:

'yyyy/MM/dd hh:mm:ss a'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM