如何使用 Boto3 獲取單個爬蟲的詳細資訊?
問題陳述 − 使用 Python 中的 boto3 庫獲取爬蟲的詳細資訊。
示例 − 獲取爬蟲 crawler_for_s3_file_job 的詳細資訊。
解決此問題的方法/演算法
步驟 1 − 匯入 boto3 和 botocore 異常以處理異常。
步驟 2 − crawler_name 是必需引數。它是一個字串,因此使用者一次只能傳送一個爬蟲名稱來獲取詳細資訊。
步驟 3 − 使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了 **region_name**。如果沒有提到,則在建立會話時顯式傳遞 **region_name**。
步驟 4 − 為 Glue 建立 AWS 客戶端。
步驟 5 − 現在使用 **get_crawler** 函式並傳遞 **crawler_name**。
步驟 6 − 它返回爬蟲的元資料。
步驟 7 − 如果在檢查作業時出現問題,請處理通用異常。
示例
使用以下程式碼來獲取爬蟲的詳細資訊:
import boto3
from botocore.exceptions import ClientError
def get_one_crawler_details(crawler_name:str)
session = boto3.session.Session()
glue_client = session.client('glue')
try:
crawler_details = glue_client.get_crawler(Name= crawler_name)
return crawler_details
except ClientError as e:
raise Exception("boto3 client error in get_one_crawler_details: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_one_crawler_details: " + e.__str__())
print(get_one_crawler_details("crawler_for_s3_file_job"))輸出
{'Crawler': {'Name': 'crawler_for_s3_file_job', 'Role': 'glue-role',
'Targets': {'S3Targets': [{'Path': 's3://test/', 'Exclusions': []}],
'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []},
'DatabaseName': 'default', 'Classifiers': [], 'SchemaChangePolicy':
{'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior':
'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'TablePrefix': 'prod_scdk_',
'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2018, 9, 24,
20, 42, 7, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 4,
27, 14, 49, 12, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED',
'LogGroup': '/aws-glue/crawlers', 'LogStream':
'crawler_for_s3_file_job', 'MessagePrefix': ************-90ad1',
'StartTime': datetime.datetime(2020, 4, 27, 14, 49, 19,
tzinfo=tzlocal())}, 'Version': 15}, 'ResponseMetadata': {'RequestId':
'8c7dcbde-***********************-774', 'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Sun, 28 Feb 2021 11:34:32 GMT', 'content-type':
'application/x-amz-json-1.1', 'content-length': '805', 'connection':
'keep-alive', 'x-amzn-requestid': '8c7dcbde-**********************774'},
'RetryAttempts': 0}}
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP