如何使用 Boto3 获取 AWS Glue 数据目录中所有可用连接的详细信息?
问题陈述-使用Python中的boto3库获取AWSGlue数据目录中存在的所有连接的详细信息。
示例-获取所有连接定义的详细信息。
解决这个问题的方法/算法
步骤1-导入boto3和botocore异常以处理异常。
步骤2-没有参数。
步骤3-使用boto3库创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name 。
第4步-为胶水创建一个AWS客户端。
第5步-调用get_connections函数。
第6步-它将从AWSGlue数据目录中获取连接定义的详细信息。
第7步-如果检查作业时出现问题,则处理通用异常。
示例
使用以下代码获取AWSGlue数据目录中所有连接的定义-
import boto3
frombotocore.exceptionsimport ClientError
def get_details_of_all_connections():
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_connections()
return response
except ClientError as e:
raise Exception("boto3 client error in get_details_of_all_connections: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_details_of_all_connections: " + e.__str__())
print(get_details_of_all_connections())输出结果{'ConnectionList': [
{'Name': '01_Daily', 'Description': '', 'ConnectionType': 'JDBC',
'ConnectionProperties': {'JDBC_CONNECTION_URL':
'jdbc:redshift://**********.us-east-1.redshift.amazonaws.com:5439/abc',
'JDBC_ENFORCE_SSL': 'false', 'PASSWORD': '!*******', 'USERNAME':
'********'}, 'PhysicalConnectionRequirements': {'SubnetId': 'subneta******e', 'SecurityGroupIdList': ['sg-********3'], 'AvailabilityZone':
'us-east-1a'}, 'CreationTime': datetime.datetime(2020, 12, 11, 17, 1,
51, 519000, tzinfo=tzlocal()), 'LastUpdatedTime':
datetime.datetime(2020, 12, 11, 17, 1, 51, 519000, tzinfo=tzlocal())},
{'Name': 'aurora-poc', 'ConnectionType': 'JDBC', 'ConnectionProperties':
{'JDBC_CONNECTION_URL': 'jdbc:postgresql://**********-cluster.clustercv********6p.us-east-1.rds.amazonaws.com:5432/*******,
'JDBC_ENFORCE_SSL': 'false', 'PASSWORD': '******', 'USERNAME': user'},
'PhysicalConnectionRequirements': {'SubnetId': 'subnet-35******e',
'SecurityGroupIdList': ['sg-*******d', 'sg-0***********'],
'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())},
{'Name': 'dev-ods', 'ConnectionType': 'JDBC', 'ConnectionProperties':
{'JDBC_CONNECTION_URL': 'jdbc:postgresql://*****************.us-east1.rds.amazonaws.com:5432/store', 'JDBC_ENFORCE_SSL': 'false',
'PASSWORD': '***********', 'USERNAME': user'},
'PhysicalConnectionRequirements': {'SubnetId': 'subnet-a********',
'SecurityGroupIdList': ['sg-*********b7', 'sg-a8********e'],
'AvailabilityZone': 'us-east-1a'}, 'CreationTime':
datetime.datetime(2020, 5, 27, 3, 10, 24, 538000, tzinfo=tzlocal()),
'LastUpdatedTime': datetime.datetime(2021, 2, 26, 5, 50, 53, 991000,
tzinfo=tzlocal())},],
'ResponseMetadata': {'RequestId': '58bae80d-*******************87',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Feb 2021
11:21:55 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '11568', 'connection': 'keep-alive', 'x-amzn-requestid':
'58bae80d-************************87'}, 'RetryAttempts': 0}}