如何使用 Boto3 獲取 AWS Glue 資料目錄中一個或多個指定爬蟲的指標?
問題陳述 − 使用 Python 中的 boto3 庫檢索指定爬蟲的指標。
示例 − 檢索指定爬蟲 crawler_for_s3_file_job 的指標。
解決此問題的方法/演算法
步驟 1 − 匯入 boto3 和 botocore 異常以處理異常。
步驟 2 − crawler_names 是必需引數。它是一個列表,因此使用者可以一次傳送一個或多個爬蟲名稱來獲取指標。
步驟 3 − 使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了 region_name。如果未提及,則在建立會話時顯式傳遞 region_name。
步驟 4 − 為 Glue 建立 AWS 客戶端。
步驟 5 − 現在使用 get_crawler_metrics 函式並傳遞 crawler_names 和 CrawlerNameList 引數。
步驟 6 − 它返回爬蟲的指標。
步驟 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}}
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP