Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách dùng Boto3 lấy chi tiết một classifier từ AWS Glue Data Catalog

Đề bài − Sử dụng thư viện boto3 trong Python để lấy chi tiết một classifier từ AWS Glue Data catalog. Ví dụ: lấy chi tiết classifier 'xml-test'.

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ố classifier_name cần kiểm tra chi tiết.

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 − Gọi get_classifier và truyền classifier_name làm tham số Name.

Bước 6 − Nó sẽ lấy chi tiết của classifier.

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 để lấy chi tiết một classifier từ AWS Glue Data catalog:

import boto3
from botocore.exceptions import ClientError

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

print(get_classifier_details("xml-test"))

Kết quả

{'Classifier': {'GrokClassifier': {'Name': 'xml-test', 'Classification': 'xml', 'CreationTime': datetime.datetime(2018, 6, 21, 4, 7, 4, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2018, 6, 21, 4, 7, 11, tzinfo=tzlocal()), 'Version': 2, 'GrokPattern': 'SYSLOGTIMESTAMP %{MONTH} +%{MONTHDAY} %{TIME}'}}, 'ResponseMetadata': {'RequestId': 'c291cce2-..............-3552077ddefd', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 21 Feb 2021 07:58:09 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '218', 'connection': 'keep-alive', 'x-amzn-requestid': 'c291cce2-...........-3552077ddefd'}, 'RetryAttempts': 0}}