在插入数据之前,我使用以下代码截断表
df.write
.option("driver", "org.postgresql:postgresql:42.2.16")
.option("truncate", True)
.jdbc(url=pgsql_connection, table="service", mode='append', properties=properties_postgres)
尽管如此,它并不起作用。该表仍包含旧数据。我使用append,因为我不想每次都删除DB并创建一个新表。
我试过.option("truncate", "true")
,但没有成功。
我没有收到错误消息。如何使用.option截断表来解决此问题。
您需要使用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->保存模式时。如果启用了覆盖,此选项将导致Spark截断现有表,而不是删除并重新创建它。