如何使用 Boto3 獲取 AWS 賬戶中所有 Schema 的列表
本文將介紹使用者如何獲取 AWS 賬戶中所有 Schema 的列表。
示例
獲取 AWS Glue 資料目錄中所有可用的 Schema 列表。
問題陳述:使用 Python 中的 boto3 庫獲取所有 Schema 的列表。
解決此問題的方法/演算法
步驟 1:匯入 boto3 和 botocore 異常以處理異常。
步驟 2:此函式沒有引數。
步驟 3:使用 boto3 庫建立 AWS 會話。確保在預設配置檔案中提到了 region_name。如果未提及,則在建立會話時顯式傳遞 region_name。
步驟 4:為 glue 建立 AWS 客戶端。
步驟 5:現在使用 list_schemas 函式
步驟 6:它返回 AWS Glue 資料目錄中所有 Schema 的列表,其中包含 Schema 的有限詳細資訊。它不包含狀態為“Deleting”的 Schema。它只包含可用 Schema 的列表。如果沒有 Schema,則返回一個空字典。
步驟 7:如果在檢查 Schema 時出現錯誤,則處理通用異常。
程式碼示例
以下程式碼獲取所有 Schema 的列表:-
import boto3
from botocore.exceptions import ClientError
def list_of_schemas()
session = boto3.session.Session()
glue_client = session.client('glue')
try:
schemas_name = glue_client.list_schemas()
return schemas_name
except ClientError as e:
raise Exception("boto3 client error in list_of_schemas: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in list_of_schemas: " + e.__str__())
print(list_of_schemas())輸出
{
'Schemas':[
{
'RegistryName': 'employee_details',
'SchemaName': 'employee_table',
'SchemaArn': 'string',
'Description': 'Schema for employees record',
'Status': 'AVAILABLE',
'CreatedTime': 'string',
'UpdatedTime': 'string'
},
{
'RegistryName': 'security_details',
'SchemaName': 'security_table',
'SchemaArn': 'string',
'Description': 'Schema for security record',
'Status': 'AVAILABLE',
'CreatedTime': 'string',
'UpdatedTime': 'string'
},
],
'Request': ……
}
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP