Đề bài − Sử dụng thư viện boto3 trong Python để xóa một bảng, được tạo trong tài khoản.
Ví dụ − Xóa bảng 'security' từ database 'test' được tạo trong tài khoản.
Cách tiếp cận/Thuật toán giải quyết
Bước 1 − Nhập boto3 và botocore exceptions để xử lý ngoại lệ.
Bước 2 − Truyền tham số database_name và table_name cần xóa từ AWS Glue Catalog.
Bước 3 − Tạo phiên AWS bằng boto3. Đảm bảo region_name trong profile mặc định. Nếu không, truyền rõ ràng region_name khi tạo phiên.
Bước 4 − Tạo client AWS cho glue.
Bước 5 − Sử dụng hàm delete_table và truyền database_name làm DatabaseName và table_name làm Name.
Bước 6 − Nó sẽ xóa bảng và trả về response metadata.
Bước 7 − Xử lý ngoại lệ chung nếu có lỗi xảy ra khi kiểm tra job.
Ví dụ
Sử dụng code sau để xóa bảng từ database 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.delete_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"))
Kết quả
{'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}}