我正在尝试从 pyspark 数据帧到 sql 表进行更新插入。
sparkdf 是我的 pyspark 数据框。测试是我在 azure sql 数据库中的 sql 表。
到目前为止我有以下内容:
def write_to_sqldatabase(final_table, target_table):
#Write table data into a spark dataframe
final_table.write.format("jdbc") \
.option("url", f"jdbc:sqlserver://{SERVER};databaseName={DATABASE}") \
.option("dbtable", f'....{target_table}') \
.option("user", USERNAME) \
.option("password", PASSWORD) \
.mode("append") \
.save()
和
spark.sql("""
merge test1 target
using test source
on source.Id = target.Id
when matched then
update set *
when not matched then insert *
""")
我尝试了不同的选择,但到目前为止没有任何效果。
根据此https://issues.apache.org/jira/browse/SPARK-19335Spark数据帧编写器API没有这样的功能。