如何使用 Boto3 停止 AWS Glue 数据目录中的爬虫
在本文中,我们将了解用户如何停止 AWS Glue 数据目录中存在的爬虫。
示例
问题陈述:使用 Python 中的 boto3 库停止爬虫。
解决此问题的方法/算法
步骤 1:导入 boto3 和 botocore 异常以处理异常。
步骤 2:crawler_name 是此函数中的参数。
步骤 3:使用 boto3 库创建 AWS 会话。确保在默认配置文件中提到了 region_name。如果未提及,则在创建会话时显式传递 region_name。
步骤 4:为 glue 创建 AWS 客户端。
步骤 5:现在使用 stop_crawler 函数并将参数 crawler_name 作为 Name 传递。
步骤 6:它返回响应元数据并在爬虫正在运行时停止它;否则它会抛出异常 – CrawlerNotRunningException。
步骤 7:如果在停止爬虫时出现错误,请处理通用异常。
示例代码
以下代码停止爬虫:
import boto3
from botocore.exceptions import ClientError
def stop_a_crawler(crawler_name)
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.stop_crawler(Name=crawler_name)
return response
except ClientError as e:
raise Exception("boto3 client error in stop_a_crawler: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in stop_a_crawler: " + e.__str__())
print(stop_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}}
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP