Đề bài − Sử dụng thư viện boto3 trong Python để lấy chi tiết tất cả connection có trong AWS Glue Data catalog.
Ví dụ − Lấy chi tiết định nghĩa tất cả connection.
Cách tiếp cận/Thuật toán giải quyết
Bước 1 − Nhập boto3 và botocore exceptions để xử lý ngoại lệ.
Bước 2 − Không có tham số.
Bước 3 − Tạo phiên AWS bằng boto3. Đảm bảo region_name trong profile mặc định. Nếu không, truyền rõ ràng region_name khi tạo phiên.
Bước 4 − Tạo client AWS cho glue.
Bước 5 − Gọi hàm get_connections.
Bước 6 − Nó sẽ lấy chi tiết định nghĩa connection từ AWS Glue Data Catalog.
Bước 7 − Xử lý ngoại lệ chung nếu có lỗi xảy ra khi kiểm tra job.
Ví dụ
Sử dụng code sau để lấy định nghĩa tất cả connection trong AWS Glue Data catalog:
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("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())
Kết quả
{'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}}