如何使用Boto3获取AWS Glue数据目录中所有连接的详细信息?

如何使用Boto3获取AWS Glue数据目录中所有连接的详细信息?

问题陈述 − 使用Python中的Boto3库获取AWS Glue数据目录中所有连接的详细信息。

示例 − 获取所有连接定义的详细信息。

更多Python相关文章,请阅读:Python 教程

解决此问题的方法/算法

步骤1 − 导入Boto3和botocore异常来处理异常。

步骤2 − 没有参数。

步骤3 − 使用Boto3库创建AWS会话。确保在默认配置文件中提供 region_name 。如果未提供,则在创建会话时显式传递 region_name

步骤4 − 为Glue创建一个AWS客户端。

步骤5 − 调用 get_connections 函数。

步骤6 − 它将从AWS Glue数据目录中获取连接定义的详细信息。

步骤7 − 处理通用异常,如果在检查作业时发生任何错误。

示例

使用以下代码获取AWS Glue数据目录中所有连接的定义信息 −

import boto3
from botocore.exceptions import 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("get_details_of_all_connections中的Boto3客户端错误:" + e.__str__())
   except Exception as e:
      raise Exception("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}}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程