如何使用 Boto3 停止 AWS Glue 資料目錄中爬蟲的排程程式


在本文中,我們將瞭解使用者如何停止 AWS Glue 資料目錄中存在的爬蟲的排程程式。

示例

停止 AWS Glue 資料目錄中可用的爬蟲的排程程式。

問題陳述:使用 Python 中的 boto3 庫來停止爬蟲的排程程式。

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

  • 步驟 1:匯入 boto3botocore 異常以處理異常。

  • 步驟 2:crawler_name 是此函式中所需的引數。

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

  • 步驟 4:glue 建立 AWS 客戶端。

  • 步驟 5:現在使用 stop_crawler_schedule 函式並將引數 crawler_name 作為 CrawlerName 傳遞。

  • 步驟 6:它返回響應元資料並將爬蟲的排程狀態設定為 OT_SCHEDULED。如果爬蟲的狀態正在執行,則它不會停止爬蟲。

  • 步驟 7:如果在停止爬蟲的排程程式時出現錯誤,則處理通用異常。

程式碼示例

以下程式碼停止爬蟲的排程程式:

import boto3
from botocore.exceptions import ClientError

def stop_scheduler_of_a_crawler(crawler_name)
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.stop_crawler_schedule(CrawlerName=crawler_name)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in stop_scheduler_of_a_crawler: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in stop_scheduler_of_a_crawler: " + e.__str__())
print(stop_scheduler_of_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}}

更新於: 2021年4月15日

106 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.