我有一個 Excel 閱讀器,我將結果放入 sparks 資料框中。我在決議時間戳時遇到問題。
我有時間戳作為字串,如Wed Dec 08 10:49:59 CET 2021. 我使用的是 spark-sql 版本2.4.5,一切正常,直到我最近更新到 version 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 version 執行相同的代碼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。我在這里錯過了一些明顯的東西嗎?
任何幫助將不勝感激。先感謝您。
uj5u.com熱心網友回復:
如日期時間模式檔案中所述,您E只能用于日期時間格式,而不能用于決議:
'E'、'F'、'q' 和'Q' 符號只能用于日期時間格式,例如date_format。它們不允許用于日期時間決議,例如 to_timestamp。
如果要應用 Spark 版本 <3.0 的行為,可以將spark.sql.legacy.timeParserPolicy選項設定為LEGACY:
sparkSession.conf.set("spark.sql.legacy.timeParserPolicy", "LEGACY")
如果您不想更改 spark 配置,可以使用substrSQL 函式洗掉代表天的字符:
import org.apache.spark.sql.functions.{col, to_timestamp, expr}
val ts: String = "Wed Dec 08 20:49:59 CET 2021"
val fmt: String = "MMM dd HH:mm:ss z yyyy"
val ttdf = Seq(ts)
.toDF("theTimestampColumn")
.withColumn("preparedTimestamp", expr("substr(theTimestampColumn, 5, length(theTimestampColumn))"))
.withColumn("parsedTime", to_timestamp(col("preparedTimestamp"), fmt = fmt))
.drop("preparedTimestamp")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/421883.html
標籤:
