如何使用Boto3檢查Glue作業是否存在?
問題陳述 − 使用Python中的boto3庫檢查Glue作業是否存在。例如,檢查run_s3_file_job是否在AWS Glue中存在。
解決此問題的方法/演算法
步驟1 − 匯入boto3和botocore異常以處理異常。
步驟2 − job_name是函式中的引數。
步驟3 − 使用boto3庫建立AWS會話。確保在預設配置檔案中提到了region_name。如果未提及,則在建立會話時顯式傳遞region_name。
步驟4 − 為Glue建立一個AWS客戶端。
步驟5 − 現在使用get_job函式並傳遞JobName。
步驟6 − 如果作業存在,則響應將包含有關作業的所有詳細資訊,否則它將引發異常。
步驟7 − 如果在檢查作業時出現問題,則處理通用異常。
示例
使用以下程式碼檢查Glue作業是否存在:
import boto3
from botocore.exceptions import ClientError
def check_glue_job_exists(job_name):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_job(JobName=job_name)
return response
except ClientError as e:
raise Exception( "boto3 client error in check_glue_job_exists: " + e.__str__())
except Exception as e:
raise Exception( "Unexpected error in check_glue_job_exists: " + e.__str__())
#To check existing job
print(check_glue_job_exists("run_s3_file_job"))
#Job doesn’t exist
print(check_glue_job_exists("run_s3_file_job_not_exist"))輸出
#To check existing job
{'Job': {'Name': 'run_s3_file_job', 'Description': 'Glue job for the
test', 'Role': 'arn:aws:iam::12345:role/delegated/glue-service-role',
'CreatedOn': datetime.datetime(2021, 02, 10, 15, 7, 3, 638000,
tzinfo=tzlocal()), 'LastModifiedOn': datetime.datetime(2021, 02, 10, 15,
7, 3, 638000, tzinfo=tzlocal()), 'ExecutionProperty':
{'MaxConcurrentRuns': 1}, 'Command': {'Name': 'glueetl',
'ScriptLocation': 's3://test/pipeline.py', 'PythonVersion': '3'},
'DefaultArguments': { '--job-language': 'python', 'Step': '0'},
'MaxRetries': 0, 'AllocatedCapacity': 4, 'Timeout': 2880, 'MaxCapacity':
4.0, 'WorkerType': 'G.1X', 'NumberOfWorkers': 4, 'GlueVersion': '2.0'},
'ResponseMetadata': {'RequestId': 'e3ec9e2c-e75d-4443-bfeafef674fff7e9', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 13
Feb 2021 13:20:27 GMT', 'content-type': 'application/x-amz-json-1.1',
'content-length': '1501', 'connection': 'keep-alive', 'x-amznrequestid': 'e3ec9e2c-e75d-4443-bfea-fef674fff7e9'}, 'RetryAttempts':
0}}
#Job doesn’t exist
botocore.errorfactory.EntityNotFoundException: An error occurred
(EntityNotFoundException) when calling the GetJob operation: Job with
name: run_s3_file_job_not_exist not found.
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP