如何使用 Boto3 从 AWS Glue 数据目录获取连接的详细信息?
问题陈述-使用Python中的boto3库获取AWSGlue数据目录中存在的连接的详细信息。
示例-获取连接定义的详细信息,“aurora-test”。
解决这个问题的方法/算法
步骤1-导入boto3和botocore异常以处理异常。
步骤2-传递需要检查其定义的参数connection_name。
步骤3-使用boto3库创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name。
第4步-为胶水创建一个AWS客户端。
第5步-调用get_connection函数并将connection_name作为Name参数传递。
第6步-它将从AWSGlue数据目录中获取连接定义的详细信息。
第7步-如果检查作业时出现问题,则处理通用异常。
示例
使用以下代码获取AWSGlue数据目录中的连接定义-
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}}