Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีใช้ Boto3 ดึงรายละเอียด Connection ทั้งหมดจาก AWS Glue Data Catalog

ปัญหา: ใช้ Boto3 ใน Python ดึงข้อมูล Connection ทั้งหมดจาก AWS Glue Data Catalog

ขั้นตอน/อัลกอริทึม

  1. Import boto3 และ botocore.exceptions.ClientError
  2. ไม่มีพารามิเตอร์ (ดึงทั้งหมด)
  3. สร้าง AWS Session และ Glue Client
  4. เรียก get_connections()
  5. คืนค่ารายการ Connection Definitions
  6. จัดการ Exception

ตัวอย่างโค้ด

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())

ผลลัพธ์

{'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', ...}, {'Name': 'dev-ods', ...}], 'ResponseMetadata': {...}}