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

awsboto3pythonserver side programmingprogramming更新于 2023/12/24 17:48:00

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

示例

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

解决此问题的方法/算法

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

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

  • 格式scheduler 应为 cron(cron_expression)。Cron_Expression 可以写为 (15 12 * * ? *),即爬虫程序将在每天 12:15UTC 运行。

  • 步骤 3:使用 boto3 lib 创建 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}}

相关文章