如何使用 Boto3 在 AWS Glue 数据目录中停止爬网程序的调度程序
在本文中,我们将了解用户如何停止AWSGlue数据目录中存在的爬网程序的调度程序。
示例
停止AWSGlue数据目录中可用的爬网程序的调度程序。
问题陈述:使用Python中的boto3库来停止爬虫的调度器。
解决这个问题的方法/算法
第一步:导入boto3和botocore异常处理异常。
第二步:crawler_name是这个函数中的必填参数。
步骤3:使用boto3lib创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name。
第4步:为glue创建一个AWS客户端。
第5步:现在使用stop_crawler_schedule函数并将参数crawler_name作为CrawlerName传递。
第6步:它返回响应元数据并将爬虫的调度状态设置为OT_SCHEDULED。如果爬虫的状态是running,那么它不会停止爬虫。
第7步:如果在停止爬虫的调度程序时出现问题,则处理通用异常。
示例代码
以下代码停止爬虫的调度程序-
import boto3 frombotocore.exceptionsimport ClientError def stop_scheduler_of_a_crawler(crawler_name) session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.stop_crawler_schedule(CrawlerName=crawler_name) return response except ClientError as e: raise Exception("boto3 client error in stop_scheduler_of_a_crawler: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in stop_scheduler_of_a_crawler: " + e.__str__()) print(stop_scheduler_of_a_crawler("Data Dimension"))输出结果
{'ResponseMetadata': {'RequestId': '73e50130-*****************8e', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Mar 2021 07:26:55 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '73e50130-***************8e'}, 'RetryAttempts': 0}}