How to use Boto3 to get the details of a single crawler?

Boto3PythonServer Side ProgrammingProgramming

Problem Statement − Use boto3 library in Python to get the details of a crawler.

Example − Get the details of a crawler, crawler_for_s3_file_job.

Approach/Algorithm to solve this problem

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

Step 2 − crawler_name is the mandatory parameter. It is a string so user can send only one crawler name at a time to fetch details.

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_crawler function and pass the crawler_name.

Step 6 − It returns the metadata of the crawler.

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

Example

Use the following code to fetch the details of a crawler −

import boto3
from botocore.exceptions import ClientError

def get_one_crawler_details(crawler_name:str)
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      crawler_details = glue_client.get_crawler(Name= crawler_name)
      return crawler_details
   except ClientError as e:
      raise Exception("boto3 client error in get_one_crawler_details: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in get_one_crawler_details: " + e.__str__())
print(get_one_crawler_details("crawler_for_s3_file_job"))

Output

{'Crawler': {'Name': 'crawler_for_s3_file_job', 'Role': 'glue-role',
'Targets': {'S3Targets': [{'Path': 's3://test/', 'Exclusions': []}],
'JdbcTargets': [], 'DynamoDBTargets': [], 'CatalogTargets': []},
'DatabaseName': 'default', 'Classifiers': [], 'SchemaChangePolicy':
{'UpdateBehavior': 'UPDATE_IN_DATABASE', 'DeleteBehavior':
'DEPRECATE_IN_DATABASE'}, 'State': 'READY', 'TablePrefix': 'prod_scdk_',
'CrawlElapsedTime': 0, 'CreationTime': datetime.datetime(2018, 9, 24,
20, 42, 7, tzinfo=tzlocal()), 'LastUpdated': datetime.datetime(2020, 4,
27, 14, 49, 12, tzinfo=tzlocal()), 'LastCrawl': {'Status': 'SUCCEEDED',
'LogGroup': '/aws-glue/crawlers', 'LogStream':
'crawler_for_s3_file_job', 'MessagePrefix': ************-90ad1',
'StartTime': datetime.datetime(2020, 4, 27, 14, 49, 19,
tzinfo=tzlocal())}, 'Version': 15}, 'ResponseMetadata': {'RequestId':
'8c7dcbde-***********************-774', 'HTTPStatusCode': 200,
'HTTPHeaders': {'date': 'Sun, 28 Feb 2021 11:34:32 GMT', 'content-type':
'application/x-amz-json-1.1', 'content-length': '805', 'connection':
'keep-alive', 'x-amzn-requestid': '8c7dcbde-**********************774'},
'RetryAttempts': 0}}
raja
Published on 23-Mar-2021 06:29:38
Advertisements