如何使用 Boto3 停止 AWS Glue 資料目錄中的爬蟲
在本文中,我們將瞭解使用者如何停止 AWS Glue 資料目錄中存在的爬蟲。
示例
問題陳述:使用 Python 中的 boto3 庫停止爬蟲。
解決此問題的方法/演算法
步驟 1:匯入 boto3 和 botocore 異常以處理異常。
步驟 2:crawler_name 是此函式中的引數。
步驟 3:使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了 region_name。如果未提及,則在建立會話時顯式傳遞 region_name。
步驟 4:為 glue 建立 AWS 客戶端。
步驟 5:現在使用 stop_crawler 函式並將引數 crawler_name 作為 Name 傳遞。
步驟 6:它返回響應元資料並在爬蟲正在執行時停止它;否則它會丟擲異常 – CrawlerNotRunningException。
步驟 7:如果在停止爬蟲時出現錯誤,請處理通用異常。
示例程式碼
以下程式碼停止爬蟲:
import boto3 from botocore.exceptions import ClientError def stop_a_crawler(crawler_name) session = boto3.session.Session() glue_client = session.client('glue') try: response = glue_client.stop_crawler(Name=crawler_name) return response except ClientError as e: raise Exception("boto3 client error in stop_a_crawler: " + e.__str__()) except Exception as e: raise Exception("Unexpected error in stop_a_crawler: " + e.__str__()) print(stop_a_crawler("Data Dimension"))
輸出
{'ResponseMetadata': {'RequestId': '73e50130-*****************8e', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Mar 2021 07:26:55 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '73e50130-***************8e'}, 'RetryAttempts': 0}}
廣告