Computer >> Máy Tính >  >> Lập trình >> Python

Làm cách nào để sử dụng Boto3 để lấy thông tin chi tiết của tất cả các bộ phân loại có sẵn trong danh mục Dữ liệu Keo AWS?

Tuyên bố sự cố:Sử dụng thư viện boto3 trong Python để nhận thông tin chi tiết về tất cả các bộ phân loại có trong danh mục Dữ liệu keo AWS. Ví dụ:lấy chi tiết của tất cả trình phân loại từ tài khoản của người dùng.

Phương pháp tiếp cận / Thuật toán để giải quyết vấn đề này

Bước 1 - Nhập các ngoại lệ boto3 và botocore để xử lý các ngoại lệ.

Bước 2 - Không có tham số.

Bước 3 - Tạo phiên AWS bằng thư viện boto3. Đảm bảo rằng tên miền được đề cập trong hồ sơ mặc định. Nếu nó không được đề cập, thì hãy chuyển rõ ràng region_name trong khi tạo phiên.

Bước 4 - Tạo ứng dụng AWS cho keo dán.

Bước 5 - Gọi get_classifiers .

Bước 6 - Nó sẽ tìm nạp thông tin chi tiết của tất cả bộ phân loại có sẵn trong Danh mục dữ liệu keo AWS.

Bước 7 - Xử lý ngoại lệ chung nếu có sự cố xảy ra trong khi kiểm tra công việc.

Ví dụ

Sử dụng mã sau để nhận chi tiết của tất cả các bộ phân loại có trong danh mục Dữ liệu keo AWS -

import boto3
from botocore.exceptions import ClientError

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

print(get_all_classifier_details())

Đầu ra

{'Classifiers': [
{'XMLClassifier': {'Name': 'aiml-linkup', 'Classification': 'xml',
'CreationTime': datetime.datetime(2020, 4, 17, 13, 26, 50,
tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 4, 17, 13, 26,
50, tzinfo=tzlocal()), 'Version': 1, 'RowTag': 'job'}},
{'XMLClassifier': {'Name': 'aiml-test1', 'Classification': 'xml',
'CreationTime': datetime.datetime(2019, 10, 7, 20, 48, 44,
tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2019, 10, 7, 20, 48,
44, tzinfo=tzlocal()), 'Version': 1, 'RowTag': 'nitf'}},
{'GrokClassifier': {'Name': 'classifier1', 'Classification':
'classifier1', '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}'}}, {'CsvClassifier': {'Name': 'csvquotes', 'CreationTime': datetime.datetime(2020, 9, 10, 5, 6, 29,
tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 9, 10, 5, 6,
29, tzinfo=tzlocal()), 'Version': 1, 'Delimiter': ',', 'QuoteSymbol':
'"', 'ContainsHeader': 'UNKNOWN', 'DisableValueTrimming': False,
'AllowSingleColumn': False}},
{'XMLClassifier': {'Name': 'xml-test', 'Classification': 'xml',
'CreationTime': datetime.datetime(2020, 4, 10, 18, 26, 50,
tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 4, 15, 0, 3,
8, tzinfo=tzlocal()), 'Version': 2, 'RowTag': 'job'}}],
'ResponseMetadata': {'RequestId': '7fa7a78e-…………e4261bfd1',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 21 Feb 2021
08:02:30 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '885', 'connection': 'keep-alive', 'x-amzn-requestid':
'7fa7a78e-……………..e4261bfd1'}, 'RetryAttempts': 0}}