如何使用 Boto3 从 AWS Data Catalog 中删除触发器?

问题陈述- 使用 Python 中的 boto3 库删除您帐户中可用的触发器。

示例- 从您的帐户中删除触发器“测试”。

解决这个问题的方法/算法

步骤 1 - 导入 boto3 和 botocore 异常以处理异常。

Step 2 - 传递应从 AWS Glue Catalog 中删除的参数 trigger_name。

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

第 4 步- 为胶水创建一个 AWS 客户端。

第 5 步- 调用 delete_trigger 并将 trigger_name 作为 Name 参数传递。

步骤 6 - 它将删除触发器并返回响应元数据。请注意,如果在 AWS Data Catalog 中未找到触发器,则不会引发任何异常。

第 7 步- 如果检查作业时出现问题,则处理通用异常。

示例

使用以下代码删除用户帐户中列出的触发器 -

import boto3
frombotocore.exceptionsimport ClientError

def delete_a_trigger(trigger_name):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.delete_trigger(Name = trigger_name)
      return response
   except ClientError as e:
      raise Exception( "boto3 client error in delete_a_trigger: " + e.__str__())
   except Exception as e:
      raise Exception( "Unexpected error in delete_a_trigger: " + e.__str__())

print(delete_a_trigger("test"))
输出结果
{'Name': 'test, 'ResponseMetadata': {'RequestId': '75abe5e2-………………….-
59a9bc617e0f', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 21
Feb 2021 05:27:11 GMT', 'content-type': 'application/x-amz-json-1.1',
'content-length': '35', 'connection': 'keep-alive', 'x-amzn-requestid':
'75abe5e2-………………59a9bc617e0f'}, 'RetryAttempts': 0}}

猜你喜欢