如何使用 Boto3 更新 AWS Glue 数据目录中爬虫的调度程序


在本文中,我们将了解如何在 AWS 账户中更新现有爬虫的调度程序。

示例

问题陈述:使用 Python 中的 boto3 库更新爬虫的调度程序。

解决此问题的方法/算法

  • 步骤 1:导入 boto3botocore 异常以处理异常。

  • 步骤 2:crawler_namescheduler 是此函数所需的必填参数。

  • scheduler 的格式应为 cron(cron_expression)Cron_Expression 可以写成 (15 12 * * ? *),即爬虫每天 UTC 时间 12:15 运行。

  • 步骤 3:使用 boto3 库创建 AWS 会话。确保在默认配置文件中提到了 region_name。如果未提及,则在创建会话时显式传递 region_name

  • 步骤 4:glue 创建 AWS 客户端。

  • 步骤 5:现在使用 update_crawler_schedule 函数并将参数 crawler_name 作为 CrawlerName 和 scheduler 作为 Schedule 传递。

  • 步骤 6:它返回响应元数据并更新爬虫的调度状态。

  • 步骤 7:如果在更新爬虫的调度程序时出现任何错误,则处理通用异常。

示例代码

以下代码更新爬虫的调度程序:

import boto3
from botocore.exceptions import ClientError

def update_scheduler_of_a_crawler(crawler_name, scheduler)
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.update_crawler_schedule(CrawlerName=crawler_name,       Schedule=scheduler)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in update_scheduler_of_a_crawler: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in update_scheduler_of_a_crawler: " + e.__str__())
print(update_scheduler_of_a_crawler("Data Dimension","cron(15 12 * * ? *)"))

输出

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

更新于: 2021年4月15日

338 次浏览

启动您的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.