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

如何配置AWS Glue将数据写入PostgreSQL患者表?

AWS Glue 写入PostgreSQL 解决方案

一、PostgreSQL/Oracle与Redshift的配置差异原因

Redshift是AWS原生托管数仓,Glue控制台做了深度集成,所以可视化配置里能直接选连接;而PostgreSQL、Oracle属于第三方数据库,Glue控制台的默认可视化目标配置只适配Glue数据目录的表,写入这类库需要通过代码或自定义配置实现。

二、具体解决步骤

1. 用Glue作业代码指定连接写入

不管是可视化生成的代码还是自定义脚本,都可以通过调用JDBC连接配置来实现写入,以下是Python示例:

import sys
from awsglue.transforms import *
from awsglue.utils import getResolvedOptions
from pyspark.context import SparkContext
from awsglue.context import GlueContext
from awsglue.job import Job

sc = SparkContext()
glueContext = GlueContext(sc)
spark = glueContext.spark_session
job = Job(glueContext)

# 加载源数据(替换成你的源表信息)
dynamic_frame = glueContext.create_dynamic_frame.from_catalog(
    database="你的Glue数据库名",
    table_name="源表名"
)

# 调用已配置的PostgreSQL连接
connection_options = glueContext.extract_jdbc_conf("你的PostgreSQL连接名称")

# 写入PostgreSQL患者表
glueContext.write_dynamic_frame.from_jdbc_conf(
    frame=dynamic_frame,
    catalog_connection="你的PostgreSQL连接名称",
    connection_options={
        "dbtable": "public.patients",  # 注意替换成你的目标表schema和表名
        "database": "PostgreSQL数据库名"
    },
    transformation_ctx="write_postgres"
)

job.commit()

2. 可视化作业的代码修改

如果是用Glue可视化界面创建的作业,默认生成的代码是写入Glue表的,你可以:

  • 切换到作业的「脚本」标签页
  • 把原本的write_dynamic_frame.from_catalog代码段替换成上面的from_jdbc_conf代码块
  • 确保作业绑定的IAM角色有权限访问这个PostgreSQL连接

3. 权限验证

  • 确认Glue作业的IAM角色:
    • 拥有glue:GetConnection权限
    • 能访问PostgreSQL所在的VPC(比如安全组开放PostgreSQL端口给Glue的IP,或者配置VPC端点)
    • 连接里的PostgreSQL账号有目标表的写入权限(INSERT、UPDATE等)

三、注意事项

  • 目标患者表需要提前在PostgreSQL中创建好,生产环境不建议自动建表
  • 核对Glue与PostgreSQL的数据类型匹配(比如Glue的int对应PostgreSQL的integer,string对应varchar)
  • 增量写入可以通过mode("append")或mode("overwrite")控制写入模式

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 02:06:08