如何使用 Boto3 从 AWS Glue 数据目录中删除爬虫?
boto3pythonserver side programmingprogramming
问题陈述 − 使用 Python 中的 boto3 库删除在您的帐户中创建的爬虫。
示例 − 删除在您的帐户中创建的爬虫"Portfolio"。
解决此问题的方法/算法
步骤 1 − 导入 boto3 和 botocore 异常来处理异常。
步骤 2 − 传递应从 AWS Glue 目录中删除的参数 crawler_name。
步骤 3 − 使用 boto3 库创建 AWS 会话。确保默认配置文件中提到了 region_name。如果没有提及,则在创建会话时明确传递region_name。
步骤4 − 为glue创建一个AWS客户端。
步骤5 − 现在使用delete_crawler函数并将crawler_name作为Name参数传递。
步骤6 − 它将删除爬虫并返回响应元数据。
步骤7 − 如果在检查作业时出现问题,则处理通用异常。
示例
使用以下代码从AWS Glue数据目录中删除爬虫 −
import boto3 from botocore.exceptions import ClientError def delete_a_crawler(crawler_name): session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.delelte_crawler(Name=crawler_name) return response except ClientError as e: raise Exception( "boto3 client error in delete_a_crawler: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in delete_a_crawler: " + e.__str__()) print(delete_a_crawler("Portfolio"))
输出
{'ResponseMetadata': {'RequestId': '067b667f-0a74d4f30a5b', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 27 Feb 2021 14:54:30 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '067b667f0a10-4f99-91be-0a74d4f30a5b'}, 'RetryAttempts': 0}}