如何使用 Boto3 獲取 AWS Glue 資料目錄中所有資料庫的詳細資訊?
問題陳述 − 使用 Python 中的 boto3 庫檢索所有資料庫的定義。
示例 − 檢索所有資料庫的定義。
解決此問題的方法/演算法
步驟 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}}
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP