Python 如何使用Boto3停止AWS Glue数据目录中爬虫的调度程序

Python 如何使用Boto3停止AWS Glue数据目录中爬虫的调度程序

在本文中,我们将看到一个用户如何停止在AWS Glue数据目录中的爬虫的调度程序。

阅读更多:Python 教程

示例

停止AWS Glue数据目录中的爬虫的调度程序。

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

解决此问题的方法/算法

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

  • 第2步: crawler_name 是此函数中必需的参数。

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

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

  • 第5步: 现在使用 stop_crawler_schedule 函数,并将参数 crawler_name 作为CrawlerName传递。

  • 第6步: 它返回响应元数据,并将爬虫的调度状态设置为 OT_SCHEDULED 。如果爬虫的状态是正在运行,则不会停止爬虫。

  • 第7步: 如果停止爬虫程序的调度程序出现问题,请处理通用异常。

示例代码

以下代码停止爬虫的调度程序 –

import boto3
from botocore.exceptions import 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}}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程