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

Cách dùng Boto3 lấy metrics của một/nhiều crawler từ AWS Glue Data Catalog

Đề bài − Sử dụng thư viện boto3 trong Python để truy xuất metrics của một crawler được chỉ định.

Ví dụ − Truy xuất metrics của 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_names là tham số bắt buộc. Đây là một list nên người dùng có thể gửi một hoặc nhiều tên crawler cùng lúc để lấy metrics.

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 get_crawler_metrics và truyền crawler_names và tham số CrawlerNameList.

Bước 6 − Nó trả về metrics 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 để truy xuất metrics của một crawler được chỉ định:

import boto3
from botocore.exceptions import ClientError

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

print(retrieves_metrics_of_a_crawler(["crawler_for_s3_file_job"]))

Kết quả

{'CrawlerMetricsList': [{'CrawlerName': crawler_for_s3_file_job, 'TimeLeftSeconds': 0.0, 'StillEstimating': False, 'LastRuntimeSeconds': 79.673, 'MedianRuntimeSeconds': 79.673, 'TablesCreated': 1, 'TablesUpdated': 0, 'TablesDeleted': 0}], 'ResponseMetadata': {'RequestId': '680cf4ca-********0abe', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Feb 2021 11:38:08 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '216', 'connection': 'keep-alive', 'x-amzn-requestid': '680cf4ca-******************0abe'}, 'RetryAttempts': 0}}