You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Glue Job跨RDS MariaDB表实现Upsert的方案咨询

AWS Glue Job实现基于非主键字段的Upsert方案(MariaDB)

最简解决方案:利用MariaDB原生语法+Glue自定义JDBC写入

因为你要基于**非主键字段central_requisition_id**做upsert,核心是让数据库能识别重复条目并触发更新,步骤如下:

  1. 给表B的central_requisition_id添加唯一索引
    MariaDB的INSERT ... ON DUPLICATE KEY UPDATE语法依赖唯一约束(主键/唯一索引)判定重复,先在表B执行这条SQL:
ALTER TABLE table_b ADD UNIQUE INDEX idx_central_requisition_id (central_requisition_id);
  1. 修改Glue Job的写入逻辑
    替换原来的普通插入代码,用自定义JDBC语句实现upsert:
# 完成schema映射后的动态帧
schema_applied_frame = ApplyMapping.apply(
    frame=some_frame,
    mappings=[
        ("supplier_id", "int", "central_parent_supplier_id", "int"),
        ("description", "string", "description", "string"),
        ("id", "int", "central_requisition_id", "int"),
    ],
    transformation_ctx="schemaapplymapping",
)

# 定义Upsert的SQL语句:存在则更新指定字段,不存在则插入
upsert_sql = """
INSERT INTO table_b (central_parent_supplier_id, description, central_requisition_id)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE
central_parent_supplier_id = VALUES(central_parent_supplier_id),
description = VALUES(description)
"""

# JDBC连接配置(替换成你的RDS实例信息)
jdbc_options = {
    "url": "jdbc:mariadb://your-rds-endpoint:3306/your-database-name",
    "user": "your-db-username",
    "password": "your-db-password",
    "customSql": upsert_sql,
    "writeMode": "append"
}

# 执行Upsert写入
schema_applied_frame.toDF().write \
    .format("jdbc") \
    .options(**jdbc_options) \
    .save()

关键说明

  • 这种方案直接复用数据库原生能力,是最简且性能最优的选择,不需要在Glue层做额外的查询对比、数据拆分操作
  • customSql参数指定带upsert逻辑的SQL,writeMode设为append即可触发批量写入
  • 确保Glue Job的IAM角色拥有RDS的连接权限,以及表结构修改权限(首次创建唯一索引时需要)

备选方案(不推荐,步骤繁琐)

如果无法修改表B结构添加唯一索引,才考虑Glue层处理:

  • 从表B读取现有central_requisition_id列表
  • 将映射后的数据集拆分为「新数据(不在列表中)」和「待更新数据(在列表中)」
  • 分别执行插入和更新操作
    这种方式需要额外读表,数据量大时性能差,仅作为极端情况的备选。

内容的提问来源于stack exchange,提问作者jan biel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 13:31:08