How to use Boto3 to get the details of multiple glue jobs at a time?

Boto3PythonServer Side ProgrammingProgramming

In this article, we will see how to get a list of resource metadata for a given list of job names.

Problem Statement − Use boto3 library in Python to get the jobs available in your account. For example, get the details of jobs those are available in your account.

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 all listed jobs for user account and then display metadata of each job.

Step 3 − Create an AWS session using boto3 library. Make sure the 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 list_jobs function to get all the jobs that are listed in user account.

Step 6 − Call batch_get_jobs and pass the job names fetched in previous function.

Step 7 − It returns list_of_jobs and metadata of each job.

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

Example

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

import boto3
from botocore.exceptions import ClientError

def get_resource_maetadata_of_glue_jobs():
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      list_of_jobs = glue_client.list_jobs()
      response = glue_client.batch_get_jobs(JobNames=list_of_jobs['JobNames'])

      return list_of_jobs, response
   except ClientError as e:
      raise Exception( "boto3 client error in get_resource_maetadata_of_glue_jobs: " + e.__str__())
   except Exception as e:
      raise Exception( "Unexpected error in get_resource_maetadata_of_glue_jobs: " + e.__str__())

a, b = get_resource_metadat_of_glue_jobs()
#List of Jobs
print(a)
#Resource metadata of each job
print(b)

Output

#List of Jobs
{'JobNames': ['01_PythonShellTest1', '01_pythonSHELL_14012021'],
'NextToken':
'eyJleHBpcmF0aW9uIjp7InNlY29uZHMiOjE2MTQxNzE2OTksIm5hbm9zIjo1MTYwMDAwMDB
9LCJsYXN0RXZhbHV
zFiMzAzNzAxMzRmNDk3NWM3M2MyMjhjYTk5MDgzZTA3YjQ0ZWEyOTZlIn19fQ==',
'ResponseMetadata': {'RequestId': '5d3eb19a-41f5-b24e-2d59ed9664b5',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Tue, 23 Feb 2021
13:01:39 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '1134', 'connection': 'keep-alive', 'x-amzn-requestid':
'5d3eb19a-41f5-b24e-2d59ed9664b5'}, 'RetryAttempts': 0}}

#Resource metadata of each job
{'Jobs': [{'Name': '01_PythonShellTest1', 'Role':
'arn:aws:iam::1234:role/dev-edl-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://test/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::1234:role/devedl-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://test/01_pythonSHELL_14012021_123.py',
'PythonVersion': '3'}, 'DefaultArguments': {'--job-bookmark-option':
'job-bookmark-disable'}, 'MaxRetries': 0, 'AllocatedCapacity': 0,
'Timeout': 2880, 'MaxCapacity': 0.0625, 'GlueVersion': '1.0'}]}

raja
Published on 22-Mar-2021 08:23:43
Advertisements