如何使用 Boto3 一次獲取所有 Glue 作業的定義?


問題陳述 − 使用 Python 中的 boto3 庫獲取使用者 AWS Glue 資料目錄中所有 Glue 作業的定義。

示例 − 獲取 AWS Glue 資料目錄中所有 Glue 作業的定義。

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

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

步驟 2 − 此函式不需要引數。它將獲取使用者帳戶所有已列出作業的定義。

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

步驟 4 − 為 Glue 建立 AWS 客戶端。

步驟 5 − 現在使用 get_jobs 函式獲取使用者帳戶中列出的所有作業的定義。請注意,list_jobsget_jobs 之間存在差異。List_jobs 僅獲取 AWS Glue 資料目錄中存在的 Glue 作業的名稱,而get_jobs 則檢索每個作業的定義。

步驟 6 − 它返回每個作業的定義。

步驟 7 − 如果在檢查作業時出現問題,則處理通用異常。

示例

使用以下程式碼獲取使用者帳戶中列出的每個作業的定義:

import boto3
from botocore.exceptions import ClientError

def get_definition_of_glue_jobs():
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.get_jobs()
      return response
   except ClientError as e:
      raise Exception("boto3 client error in get_definition_of_glue_jobs: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in get_definition_of_glue_jobs: " + e.__str__())
print(get_definition_of_glue_jobs())

輸出

{'Jobs': [
{'Name': '01_PythonShellTest1', 'Role': 'arn:aws:iam::********:role/devedl-glue-role', 'CreatedOn': datetime.datetime(2021, 1, 6, 19, 59, 19,
387000, tzinfo=tzlocal()), 'LastModifiedOn': datetime.datetime(2021, 2, 9,
21, 47, 31, 614000, tzinfo=tzlocal()), 'ExecutionProperty':
{'MaxConcurrentRuns': 1}, 'Command': {'Name': 'pythonshell',
'ScriptLocation': 's3://ivz-dev-ds-staging-
/01_pythonShellTest/test1/01_PythonShellTest1.py', 'PythonVersion': '3'},
'DefaultArguments': {'--job-bookmark-option': 'job-bookmark-disable', '--
job-language': 'python'}, 'MaxRetries': 0, 'AllocatedCapacity': 0,
'Timeout': 2880, 'MaxCapacity': 0.0625, 'GlueVersion': '1.0'},
{'Name': '01_pythonSHELL_14012021', 'Role':
'arn:aws:iam::*********:role/dev-edl-glue-role', 'CreatedOn':
datetime.datetime(2021, 1, 14, 20, 22, 40, 965000, tzinfo=tzlocal()),
'LastModifiedOn': datetime.datetime(2021, 1, 14, 20, 22, 40, 965000,
tzinfo=tzlocal()), 'ExecutionProperty': {'MaxConcurrentRuns': 1},
'Command': {'Name': 'pythonshell', 'ScriptLocation': 's3://ivz-dev-dsstaging-work/ /test_14012021/01_pythonSHELL_14012021_123.py',
'PythonVersion': '3'}, 'DefaultArguments': {'--job-bookmark-option': 'jobbookmark-disable'}, 'MaxRetries': 0, 'AllocatedCapacity': 0, 'Timeout':
2880, 'MaxCapacity': 0.0625, 'GlueVersion': '1.0'},
{'Name': 'GlueConnectionTest', 'Role': 'arn:aws:iam::*********:role/devedl-qa-automation-glue-role', 'CreatedOn': datetime.datetime(2020, 3, 6,
16, 27, 3, 862000, tzinfo=tzlocal()), 'LastModifiedOn':
datetime.datetime(2020, 3, 6, 16, 49, 19, 942000, tzinfo=tzlocal()),
'ExecutionProperty': {'MaxConcurrentRuns': 1}, 'Command': {'Name':
'pythonshell', 'ScriptLocation': 's3://glue-job-connection-testbucket/test.py', 'PythonVersion': '3'}, 'DefaultArguments': {'--jobbookmark-option': 'job-bookmark-disable', '--job-language': 'python'},
'Connections': {'Connections': ['dev-edl-redshift-glue-connection', 'devedl-rds-glue-connection']}, 'MaxRetries': 0, 'AllocatedCapacity': 0,
'Timeout': 2880, 'MaxCapacity': 0.0625, 'GlueVersion': '1.0'}] 'NextToken':
'eyJleHBpcmF0aW9uIjp7InNlY29uZHMiOjE2MTQ2NjI2MTAsIm5hbm9zIjo2MjEwMDAwMDB9LC
JsYXN0RXZhbHVhdGVkS2V5Ijp7ImpvYk5hbWUiOnsicyI6InRpY2stZGF0YS10ZXN0In0sImFjY
291bnRJZCI6eyJzIjoiNzgyMjU4NDg1ODQxIn0sImpvYklkIjp7InMiOiJqXzhmZDc2MzhkYTcy
MGQ5Yzk1YTg4MTlkOTgxNTE5MDdjM2JmYWI3ZGYxNGUwNGExZGM0ZTIzZjViYjczM2M2ZTYifX1
9', 'ResponseMetadata': {'RequestId': '287ad828-
**************************af', 'HTTPStatusCode': 200, 'HTTPHeaders':
{'date': 'Mon, 01 Mar 2021 05:23:30 GMT', 'content-type': 'application/xamz-json-1.1', 'content-length': '8258', 'connection': 'keep-alive', 'xamzn-requestid': '287ad828-*****************af'}, 'RetryAttempts': 0}}

更新於: 2021年3月23日

460 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.