How to use Boto3 to get the definition of all the Glue jobs at a time?


Problem Statement − Use boto3 library in Python to get definition of all glue jobs present in user’s AWS Glue Data catalog.

Example − Get definition of all glue jobs present in AWS Glue Data catalog.

Approach/Algorithm to solve this problem

Step 1 − Import boto3 and botocore exceptions to handle exceptions.

Step 2 − No parameter is required for this function. It will fetch definition of all listed jobs for user account.

Step 3 − Create an AWS session using boto3 library. Make sure region_name is mentioned in default profile. If it is not mentioned, then explicitly pass the region_name while creating the session.

Step 4 − Create an AWS client for glue.

Step 5 − Now use get_jobs function to get definition of all jobs those are listed in user account. Note that there is a difference in list_jobs and get_jobs. List_jobs just fetches the names of glue jobs present in AWS Glue Data catalog while get_jobs retrives definition of each job.

Step 6 − It returns the definition of each job.

Step 7 − Handle the generic exception if something went wrong while checking the job.

Example

Use the following code to fetch definition of each job listed in user account −

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())

Output

{'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}}

Updated on: 23-Mar-2021

410 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements