【发布时间】:2022-01-27 13:31:01
【问题描述】:
我有一个 Excel 阅读器,我将结果放入 sparks 数据框中。我在解析时间戳时遇到问题。
我有像Wed Dec 08 10:49:59 CET 2021 这样的字符串形式的时间戳。我使用的是 spark-sql 版本 2.4.5,一切正常,直到我最近更新到版本 3.1.2。
请在下面找到一些最少的代码。
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions.{col, to_timestamp}
val ts: String = "Wed Dec 08 20:49:59 CET 2021"
val oldfmt: String = "E MMM dd HH:mm:ss z yyyy"
val ttdf = Seq(ts)
.toDF("theTimestampColumn")
.withColumn("parsedTime", to_timestamp(col("theTimestampColumn"), fmt = oldfmt))
ttdf.show()
使用 spark 版本 2.4.5 运行此代码可以正常工作并产生以下输出:
+--------------------+-------------------+
| theTimestampColumn| parsedTime|
+--------------------+-------------------+
|Wed Dec 08 20:49:...|2021-12-08 20:49:59|
+--------------------+-------------------+
现在,仅使用 spark 版本 3.1.2 执行相同的代码会导致以下错误:
Exception in thread "main" org.apache.spark.SparkUpgradeException:
You may get a different result due to the upgrading of Spark 3.0:
Fail to recognize 'E MMM dd HH:mm:ss z yyyy' pattern in the DateTimeFormatter.
1) You can set spark.sql.legacy.timeParserPolicy to LEGACY to restore the behavior before 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
(可点击链接:https://spark.apache.org/docs/latest/sql-ref-datetime-pattern.html)
这个网站对我没有进一步的帮助。我在我的格式字符串中没有发现任何错误。
符号E 将day-of-week 表示为类似Tue; Tuesday 的文本。
符号M 代表month-of-year,如7; 07; Jul; July。符号H,m,s,y 分别是小时、分钟、秒或年。符号z 表示time-zone name,如Pacific Standard Time; PST。
我在这里错过了什么明显的东西吗?
任何帮助将不胜感激。提前谢谢你。
【问题讨论】:
-
我在这里回答了类似的问题:stackoverflow.com/questions/70597174/…
标签: scala apache-spark parsing timestamp spark3