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

Cách dùng Boto3 lấy chi tiết nhiều crawler cùng lúc

Ví dụ: Lấy chi tiết crawler crawler_for_s3_file_job.

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 − crawler_name là tham số bắt buộc. Là list nên người dùng có thể gửi nhiều tên crawler cùng lúc để lấy chi tiết.

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 − Sử dụng hàm batch_get_crawlers và truyền crawler_names.

Bước 6 − Nó trả về metadata của các crawler.

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 chi tiết các crawler:

import boto3
from botocore.exceptions import ClientError

def get_crawler_details(crawler_names:list):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        crawler_details = glue_client.batch_get_crawlers(CrawlerNames=crawler_names)
        return crawler_details
    except ClientError as e:
        raise Exception("boto3 client error in get_crawler_details: " + e.__str__())
    except Exception as e:
        raise Exception("Unexpected error in get_crawler_details: " + e.__str__())

print(get_crawler_details(["crawler_for_s3_file_job"]))