我正在使用以下代碼在插入資料之前截斷表。
df.write \
.option("driver", "org.postgresql:postgresql:42.2.16") \
.option("truncate", True) \
.jdbc(url=pgsql_connection, table="service", mode='append', properties=properties_postgres)
雖然,它不起作用。該表仍然帶有舊資料。我正在使用追加,因為我不想每次都洗掉資料庫并創建一個新表。
我試過.option("truncate", "true")但也沒有奏效。
我沒有收到錯誤訊息。我如何使用 .option 截斷我的表來解決這個問題。
uj5u.com熱心網友回復:
你需要使用overwrite模式
df.write \
.option("driver", "org.postgresql:postgresql:42.2.16") \
.option("truncate", True) \
.jdbc(url=pgsql_connection, table="service", mode='overwrite', properties=properties_postgres)
如檔案中所述
https://spark.apache.org/docs/latest/sql-data-sources-jdbc.html
truncate: true -> 當啟用 SaveMode.Overwrite 時,此選項會導致 Spark 截斷現有表,而不是洗掉并重新創建它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383924.html
上一篇:使用lambda、python在PySpark中進行元組操作
下一篇:pyspark中不存在忽略路徑
