Đề bài − Sử dụng thư viện boto3 trong Python để xóa bảng phiên bản cụ thể, được tạo trong tài khoản.
Ví dụ − Xóa bảng 'security' phiên bản 1 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, table_name và version_id 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_version và truyền database_name làm DatabaseName, table_name làm TableName và version_id làm VersionId. Lưu ý VersionId là chuỗi nên truyền trong dấu nháy đơn. Người dùng không thể xóa phiên bản mới nhất của bảng. Chỉ xóa được phiên bản cũ. Nếu muốn xóa phiên bản mới nhất, hãy dùng hàm delete_table.
Bước 6 − Nó sẽ xóa phiên bản cũ cụ thể củ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 phiên bản cụ thể của bảng từ database AWS Glue:
import boto3
from botocore.exceptions import ClientError
def delete_a_table_version_from_database(database_name, table_name, version_id:str):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.delete_table_version(DatabaseName=database_name, Name=table_name, VersionId=version_id)
return response
except ClientError as e:
raise Exception("boto3 client error in delete_a_table_version_from_database: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in delete_a_table_version_from_database: " + e.__str__())
print(delete_a_table_version_from_database("test", "security", "1"))
Kết quả
{'ResponseMetadata': {'RequestId': '7aa7c3cb...........-8a238c', '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}}