Syntax error How to use Boto3 get the details of all the databases from AWS Glue Data Catalog?

How to use Boto3 get the details of all the databases from AWS Glue Data Catalog?



Problem Statement − Use boto3 library in Python to retrieve the definition of all the databases.

Example − Retrieve the definition of all the databases.

Approach/Algorithm to solve this problem

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

Step 2 − There is no parameter.

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_databases function.

Step 6 − It returns the definition of all databases present in user’s account.

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

Example

Use the following code to retrieve the definition of all the databases −

import boto3
from botocore.exceptions import ClientError

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

Output

{'DatabaseList': [
{'Name': 'QA-test', 'CreateTime': datetime.datetime(2020, 11, 18, 14,
24, 46, tzinfo=tzlocal())},
{'Name': 'custdb', 'CreateTime': datetime.datetime(2020, 8, 31, 20, 30,
9, tzinfo=tzlocal())},
{'Name': 'default', 'Description': 'Default Hive database',
'LocationUri': 'hdfs://ip-
************.ec2.internal:****/user/hive/warehouse', 'CreateTime':
datetime.datetime(2018, 5, 25, 16, 4, 54,
tzinfo=tzlocal())},'NextToken':
'eyJsYXN0RXZhbHVhdGVkS2V5Ijp7IkhBU0hfS0VZIjp7InMiOiJuLjc4MjI1ODQ4NTg0MSJ
9LCJSQU5HRV9LRVkiOnsicyI6InN************Mjk3NywibmFub3MiOjIyNTA*********
**NvbnRleHQiOmZhbHNlfQ==',
'ResponseMetadata': {'RequestId': 'fa0a2069-***********-a0617',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Feb 2021
12:49:37 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '25749', 'connection': 'keep-alive', 'x-amzn-requestid':
'fa0a2069-************a0617'}, 'RetryAttempts': 0}}
Updated on: 2021-03-23T06:35:20+05:30

717 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements