如何使用Boto3获取来自AWS Glue Data Catalog的所有数据库的详细信息?
问题陈述 − 使用 Python 中的 boto3 库检索所有数据库的定义。
示例 − 检索所有数据库的定义。
更多Python相关文章,请阅读:Python 教程
解决这个问题的方法/算法
步骤1 − 导入 boto3 和 botocore 异常以处理异常。
步骤2 − 没有参数。
步骤3 − 使用 boto3 库创建 AWS 会话。确保在默认配置文件中提到了region_name。如果没有提到,则在创建会话时显式传递 region_name。
步骤4 − 为 Glue 创建 AWS 客户端。
步骤5 − 现在使用 get_databases 函数。
步骤6 − 它返回用户帐户中存在的所有数据库的定义。
步骤7 − 处理通用异常,如果在检查作业时出现问题。
示例
使用以下代码检索所有数据库的定义 –
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())
输出
{'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}}