如何使用 Boto3 从 AWS Glue 数据目录中获取一个或多个指定爬虫的指标?
boto3pythonserver side programmingprogramming更新于 2024/1/10 21:44:00
问题陈述 − 使用 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}}