如何使用 Boto3 在 AWS Glue 数据目录中启动爬网程序的调度程序
在本文中,我们将了解用户如何在AWSGlue数据目录中启动爬网程序的调度程序。
示例
启动AWSGlue数据目录中可用的爬网程序的调度程序。
问题陈述:使用Python中的boto3库来启动爬虫的调度器。
解决这个问题的方法/算法
第一步:导入boto3和botocore异常处理异常。
第二步:crawler_name是这个函数中的必填参数。
步骤3:使用boto3lib创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name。
第4步:为glue创建一个AWS客户端。
第5步:现在使用start_crawler_schedule函数并将参数crawler_name作为CrawlerName传递。
第六步:返回响应元数据,设置爬虫的调度状态为SCHEDULED。如果爬虫的状态是running或者调度状态已经是SCHEDULED,那么它会抛出异常-SchedulerRunningException。
第7步:如果在启动爬虫调度程序时出现问题,则处理通用异常。
示例代码
以下代码启动爬虫的调度程序-
import boto3 frombotocore.exceptionsimport ClientError def start_scheduler_of_a_crawler(crawler_name) session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.start_crawler_schedule(CrawlerName=crawler_name) return response except ClientError as e: raise Exception("boto3 client error in start_scheduler_of_a_crawler: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in start_scheduler_of_a_crawler: " + e.__str__()) print(start_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}}