当我进行此调用时(尝试从 AWS Glue 获取不存在的表):
glueContext.create_dynamic_frame.from_catalog(database='wrong_database_name', table_name='or_wrong_table_name')
我遇到这样的异常
调用 o79.getCatalogSource 时发生错误。 :com.amazonaws.services.glue.model.EntityNotFoundException:找不到实体(服务:AWSGlue;状态代码:400;错误代码:EntityNotFoundException;请求 ID:4f26a2ed-c7cc-48d4-bfe3-101816abe56d;代理:null
我该如何捕获这个异常? 我尝试了这两种方法,但都不起作用:
import botocore
try:
glueContext.create_dynamic_frame.from_catalog(database='wrong_database_name',
table_name='or_wrong_table_name')
except botocore.exceptions.ClientError as err:
if err.response['Error']['Code'] == 'EntityNotFoundException':
# ...
import boto3
glue = boto3.client('glue')
try:
glueContext.create_dynamic_frame.from_catalog(database='wrong_database_name',
table_name='or_wrong_table_name')
except glue.exceptions.EntityNotFoundException:
# ...
第二个向异常的 __ init __ 方法询问两个参数,因此这看起来很奇怪/不是解决问题的方法.. 还有人遇到过这个吗?
我遇到了同样的问题,似乎错误发生在 AWS Glue/Spark 环境中的 Java 级别。我的解决方案不是直接捕获 EntityNotFoundException,而是捕获 Py4JJavaError 并区分 except 块中的特定错误。
from py4j.protocol import Py4JJavaError
try:
glueContext.create_dynamic_frame.from_catalog(database='wrong_database_name',
table_name='or_wrong_table_name')
except Py4JJavaError as e:
# Catch and process the Py4JJavaError
if 'EntityNotFoundException' in str(e):
print("EntityNotFoundException: The requested table was not found.")
else:
print(f"Unexpected Py4J error: {e}")
它包含在boto3中。只需做:
import boto3
raise EntityNotFoundException