如何使用 Boto3 從 AWS Glue 資料目錄中刪除表?
問題陳述 − 在 Python 中使用 boto3 庫刪除在您的賬戶中建立的表。
示例 − 從您的賬戶中建立的資料庫“test”中刪除名為“security”的表。
解決此問題的方法/演算法
步驟 1 − 匯入 boto3 和 botocore 異常以處理異常。
步驟 2 − 傳遞應從 AWS Glue Catalog 中刪除的資料庫名稱和表名稱引數。
步驟 3 − 使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了 region_name。如果未提及,則在建立會話時顯式傳遞 region_name。
步驟 4 − 為 Glue 建立 AWS 客戶端。
步驟 5 − 現在使用 delete_table 函式,並將 database_name 作為 DatabaseName 引數傳遞,並將 table_name 作為 Name 引數傳遞。
步驟 6 − 它將刪除該表並返回響應元資料。
步驟 7 − 如果在檢查作業時出現問題,請處理通用異常。
示例
使用以下程式碼從 AWS Glue 資料庫中刪除表:
import boto3
from botocore.exceptions import ClientError
def delete_a_table_from_database(database_name, table_name):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.delelte_table(DatabaseName= database_name, Name = table_name)
return response
except ClientError as e:
raise Exception( "boto3 client error in delete_a_table_from_database: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in delete_a_table_from_database: " + e.__str__())
print(delete_a_table_from_database("test","security"))輸出
{'ResponseMetadata': {'RequestId': '7aa7c3cb…………….-8610318a238c',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 21 Feb 2021
04:58:04 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '7aa7c3cbf611-494e-b923-8610318a238c'}, 'RetryAttempts': 0}}
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP