AWS Glue在读取具有大写的表名和列名的PostgreSQL数据库时出现问题。

0

【以下的问题经过翻译处理】 我有一个使用大写字符的表名和列名的RDS PostgreSQL数据库。我创建了一个Glue爬虫,通过JDBC连接到数据库,并在转化为数据目录时将大写字符转换为小写字符。当我运行Glue作业进行查询时,出现以下错误:

An error occurred while calling o94.getDynamicFrame. ERROR: column "id" does not exist

我复制了这个表,并将表名和列名改为全小写字符,然后相同的Glue作业和查询成功运行。

在我们的生产环境中将表名和列名改为小写字符并不是一个可行的选择,因为这需要大量的工作。

我在Glue UI中找到了“编辑模式”选项,可以更改列名和数据类型。但是,当您将字符更改为大写字母,然后选择“保存”时,它会被还原为小写字母。

我直接编辑了pyspark脚本,并使用additional_options参数与glueContext.create_dynamic_frame.from_catalog方法一起使用来构建我的select语句,包括使用大写和小写字符。但仍然出现以上提到的错误信息。

为 PostgreSQL 表生成的脚本: PostgreSQLtable_node1 = glueContext.create_dynamic_frame.from_catalog( database="mydatabase", table_name="mytable", additional_options={"query":"SELECT id from mytable;"}, transformation_ctx="PostgreSQLtable_node1"

我认为失败的原因是数据目录中存储的模式包含小写字符,而实际的模式在数据库中使用大写字符,因此当Glue尝试使用该表时,它正在寻找“id”,而实际上是“ID”,因此会返回“未找到”错误

1 Antwort
0

【以下的回答经过翻译处理】 我认为问题不在于作业标记,而是传递给PG实例的查询。在这里使用from_catalog方法查询数据库是行不通的。当我们使用from_catalog方法创建DynamicFrame时,Glue将首先查询列名,并检查PG实例。Glue会将列名折叠为小写,然后传递给PG实例。由于PG是大小写敏感的,它将尝试按名称ID搜索列,但找不到该列,因此会引发错误。

相反,我们需要直接从PG实例中读取,使用from_options方法。我请求您添加查询,但要将大小写敏感的列从查询中转义,以使用确切的列名以便Glue不将其折叠为小写,而PG引擎将能够在表中找到必需的列。以下是如何做到这一点的方法。

示例代码片段:

从 PG 读取

connection_pg_options = { "url": "jdbc:postgresql://<jdbc-host-name>:5432/db", "dbtable": "public.test", "user": "admin", "password": "pwd", "sampleQuery": "select "ID" from public.test", }

PGNode = glueContext.create_dynamic_frame.from_options(connection_type="postgresql", connection_options=connection_pg_options)

PGNode.show()

我在我的端上测试了这个PG模式。

create table with_case ("ID" int PRIMARY KEY, "firstName" varchar(10), "lastName" varchar(10));

Glue读取代码片段:

connection_pg_options = { "url": "jdbc:postgresql://.mydatabase.us-west-2.rds.amazonaws.com:5432/pilot", "dbtable": "public.with_case", "user": "postgres", "password": "mypassword", "sampleQuery": "select "ID" from public.with_case" }

PGNode = glueContext.create_dynamic_frame.from_options(connection_type="postgresql", connection_options=

profile picture
EXPERTE
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen