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

วิธีใช้ Boto3 ดึงเมตริก (Metrics) ของ Crawler หนึ่งหรือหลายตัวจาก AWS Glue Data Catalog

ข้อกำหนดปัญหา: ใช้ไลบรารี Boto3 ใน Python เพื่อดึงเมตริกของ Crawler ที่ระบุ

ตัวอย่าง: ดึงเมตริกของ Crawler ชื่อ 'crawler_for_s3_file_job'

ขั้นตอนการแก้ปัญหา

  1. ขั้นตอน 1: นำเข้า boto3 และ botocore exceptions
  2. ขั้นตอน 2: crawler_names เป็นพารามิเตอร์บังคับ เป็น List ดังนั้นผู้ใช้สามารถส่งชื่อ Crawler ได้หนึ่งหรือหลายตัวพร้อมกัน
  3. ขั้นตอน 3: สร้าง AWS session ด้วย Boto3
  4. ขั้นตอน 4: สร้าง AWS client สำหรับ Glue
  5. ขั้นตอน 5: ใช้ฟังก์ชัน get_crawler_metrics ส่ง crawler_names และ CrawlerNameList parameter
  6. ขั้นตอน 6: คืนค่าเมตริกของ Crawlers
  7. ขั้นตอน 7: จัดการข้อยกเว้นทั่วไป

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

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"]))

ผลลัพธ์

{'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}}