问题陈述- 使用 Python 中的 boto3 库获取 AWS Glue 数据目录中存在的连接的详细信息。
示例- 获取连接定义的详细信息,“aurora-test”。
步骤 1 - 导入 boto3 和 botocore 异常以处理异常。
步骤 2 - 传递需要检查其定义的参数 connection_name。
步骤 3 - 使用 boto3 库创建 AWS 会话。确保在默认配置文件中提到 region_name。如果未提及,则在创建会话时显式传递 region_name。
第 4 步- 为胶水创建一个 AWS 客户端。
第 5 步- 调用get_connection函数并将 connection_name 作为Name参数传递。
第 6 步- 它将从 AWS Glue 数据目录中获取连接定义的详细信息。
第 7 步- 如果检查作业时出现问题,则处理通用异常。
使用以下代码获取 AWS Glue 数据目录中的连接定义 -
import boto3 frombotocore.exceptionsimport ClientError def get_details_of_a_connection(connection_name): session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.get_connection(Name= connection_name) return response except ClientError as e: raise Exception("boto3 client error in get_details_of_a_connection: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in get_details_of_a_connection: " + e.__str__()) print(get_details_of_a_connection("aurora-poc"))输出结果
{'Connection': {'Name': 'aurora-poc', 'ConnectionType': 'JDBC', 'ConnectionProperties': {'JDBC_CONNECTION_URL': 'jdbc:postgresql://abcpostgresql-cluster.cluster-abc.us-east-1.rds.amazonaws.com:0132/abc, 'JDBC_ENFORCE_SSL': 'false', 'PASSWORD': '******', 'USERNAME': 'abc***'}, 'PhysicalConnectionRequirements': {'SubnetId': 'subnet351*****', 'SecurityGroupIdList': ['sg-caa******', 'sg-*************'], 'AvailabilityZone': 'us-east-1c'}, 'CreationTime': datetime.datetime(2020, 11, 18, 12, 38, 29, 625000, tzinfo=tzlocal()), 'LastUpdatedTime': datetime.datetime(2020, 11, 18, 12, 51, 16, 59000, tzinfo=tzlocal())}, 'ResponseMetadata': {'RequestId': '6f13524b-4175- 454b-bc60-c7f408967098', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Feb 2021 11:19:18 GMT', 'content-type': 'application/x-amzjson-1.1', 'content-length': '523', 'connection': 'keep-alive', 'x-amznrequestid': '6f13524b-*****************7098'}, 'RetryAttempts': 0}}