Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách dùng Boto3 xóa trigger từ AWS Data Catalog

Đề bài − Sử dụng thư viện boto3 trong Python để xóa một trigger có sẵn trong tài khoản.

Ví dụ − Xóa trigger 'test' từ tài khoản.

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 − Truyền tham số trigger_name cần xóa từ AWS Glue Catalog.

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 delete_trigger và truyền trigger_name làm tham số Name.

Bước 6 − Nó sẽ xóa trigger và trả về response metadata. Lưu ý, nếu trigger không tìm thấy trong AWS Data Catalog, nó không ném ngoại lệ.

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 để xóa trigger liệt kê trong tài khoản:

import boto3
from botocore.exceptions import ClientError

def delete_a_trigger(trigger_name):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.delete_trigger(Name=trigger_name)
        return response
    except ClientError as e:
        raise Exception("boto3 client error in delete_a_trigger: " + e.__str__())
    except Exception as e:
        raise Exception("Unexpected error in delete_a_trigger: " + e.__str__())

print(delete_a_trigger("test"))

Kết quả

{'Name': 'test', 'ResponseMetadata': {'RequestId': '75abe5e2-................-59a9bc617e0f', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 21 Feb 2021 05:27:11 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '35', 'connection': 'keep-alive', 'x-amzn-requestid': '75abe5e2-..............59a9bc617e0f'}, 'RetryAttempts': 0}}