我發現我的業務代碼有一些非法資料,經過除錯,我發現這個bug是spark磁區解決引起的,我應該怎么做才能避免這個問題而不改變寫磁區列。
import org.apache.spark.sql.functions.lit
import spark.implicits._
val df = Seq(("122D", 2), ("122F", 2), ("122", 2))
.toDF("no", "value")
.withColumn("other", lit(1))
val path = "/user/my/output"
df
.write
.partitionBy("no","value")
.parquet(path)
我的預期結果讀作 aame as write
df.show()
---- ----- -----
| no|value|other|
---- ----- -----
|122D| 2| 1|
|122F| 2| 1|
| 122| 2| 1|
---- ----- -----
// df.distinct.count==3
像這樣的實際讀取結果
val read=spark.read.parquet(path)
read.show()
----- ----- -----
|other| no|value|
----- ----- -----
| 1|122.0| 2|
| 1|122.0| 2|
| 1|122.0| 2|
----- ----- -----
// read.distinct.count==1
檢查output
目錄結構是這樣的
└─output
├─no=122
│ └─value=2
├─no=122D
│ └─value=2
└─no=122F
└─value=2
非常感謝。我的 spark 版本是2.4.5
,scala 版本是2.11.12
uj5u.com熱心網友回復:
只需添加 spark.conf.set("spark.sql.sources.partitionColumnTypeInference.enabled",false)
uj5u.com熱心網友回復:
對于理論知識:所有內置檔案源(包括 Text/CSV/JSON/ORC/Parquet)都能夠自動發現和推斷磁區資訊。磁區列的資料型別是自動推斷的。
您可以使用:spark.sql.sources.partitionColumnTypeInference.enabled
作為 False。
確保:當型別推斷被禁用時,字串型別將用于磁區列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/400121.html