如何使用 Boto3 獲取 AWS Glue 資料目錄中所有分類器的詳細資訊?


問題陳述:使用 Python 中的 boto3 庫獲取 AWS Glue 資料目錄中所有存在的分類器的詳細資訊。例如,獲取使用者帳戶中的所有分類器的詳細資訊。

解決此問題的方法/演算法

步驟 1 - 匯入 boto3 和 botocore 異常以處理異常。

步驟 2 - 沒有引數。

步驟 3 - 使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了 region_name。如果未提及,則在建立會話時顯式傳遞 region_name。

步驟 4 - 為 glue 建立 AWS 客戶端。

步驟 5 - 呼叫 get_classifiers

步驟 6 - 它將獲取 AWS Glue 資料目錄中所有可用分類器的詳細資訊。

步驟 7 - 如果在檢查作業時出現任何錯誤,請處理通用異常。

示例

使用以下程式碼獲取 AWS Glue 資料目錄中所有存在的分類器的詳細資訊:

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())

輸出

{'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}}

更新於: 2021年3月23日

111 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

立即開始
廣告

© . All rights reserved.