使用 Glue 连接和 spark scala 覆盖 Mysql table

Overwrite Mysql table using Glue connection and spark scala

是否可以使用预定义的连接在 aws glue 作业中执行预操作查询? 或者如何使用 glueContext.getJDBCSink?

覆盖 mysql table 中的数据

我要执行的代码是

val datasink4 = glueContext.getJDBCSink(
catalogConnection = "xxxxx_mysql",
options = JsonOptions(
"""{"dbtable": "xxxxx.role_code_se", 
"database": "xxxxx", 
"preactions": "TRUNCATE TABLE xxxxx.role_code_se;", 
"overwrite": "true"}"""
), 
redshiftTmpDir = "", transformationContext = "datasink4"
).writeDynamicFrame(new_dynamic_frame)

但它不起作用。它忽略覆盖和截断选项并抛出错误

java.sql.BatchUpdateException: Duplicate entry '31' for key 'ix_role_code_se_role_code' at

Glue 只允许使用 redshift 进行前动作和后动作,而不允许其他 databases.If 您想要覆盖 table 然后将动态帧转换为数据帧然后使用如下内容:

df.write.option("truncate", "true").jdbc(url=DATABASE_URL, table=DATABASE_TABLE, mode="overwrite", properties=DATABASE_PROPERTIES)

示例参考this to know more about spark jdbc options and this