如何使用 Boto3 從 AWS 資料目錄獲取資料庫中多個函式定義的詳細資訊
讓我們看看使用者如何從 AWS Glue 資料目錄獲取多個函式定義的詳細資訊。
示例
問題陳述:使用 Python 中的 **boto3** 庫從 AWS Glue 資料目錄獲取資料庫中存在的多個函式定義的詳細資訊。
解決此問題的方法/演算法
步驟 1:匯入 **boto3** 和 **botocore** 異常以處理異常。
步驟 2:**database_name** 和 regular_pattern 是可選引數。如果未為此提供詳細資訊,則該函式將獲取 AWS 使用者帳戶中所有現有函式的定義。如果提供了 **database_name** 但未提供 **regular_pattern**,則它將獲取給定資料庫中所有函式的定義。如果提供了這兩個引數,則它將根據 **regular_pattern** 獲取匹配函式的定義。如果只提供了 **regular_pattern**,則它將獲取 AWS 使用者帳戶中與 **regular_pattern** 匹配的所有函式的定義。
步驟 3:使用 **boto3 lib** 建立 AWS 會話。確保在預設配置檔案中提到了 **region_name**。如果沒有提到,則在建立會話時顯式傳遞 **region_name**。
步驟 4:為 **glue** 建立 AWS 客戶端。
步驟 5:呼叫 **get_multiple_function_definition** 並將 **database_name** 作為 DatabaseName 引數和 **regular_pattern** 作為 Pattern 引數傳遞。
步驟 6:它根據提供的引數返回多個函式的定義。
步驟 7:如果在檢查函式時出現問題,請處理通用異常。
示例程式碼
以下程式碼獲取多個函式的定義:
import boto3
from botocore.exceptions import ClientError
def get_multiple_function_definition(database_name =None, regular_pattern = None):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_user_defined_functions(DatabaseName=database_name,Pattern= regular_pattern)
return response
except ClientError as e:
raise Exception("boto3 client error in get_multiple_function_definition: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_multiple_function_definition: " + e.__str__())
a = get_multiple_function_definition('employee')
print(a)輸出
{
'UserDefinedFunctions':[{
'FunctionName': 'insert_employee_record',
'DatabaseName': 'employee',
'ClassName': 'InsertEmployee',
'OwnerName': 'string',
'OwnerType': 'USER'|'ROLE'|'GROUP',
'CreateTime': datetime(2021,03,15),
'ResourceUris':[
{
'ResourceType': 'JAR'|'FILE'|'ARCHIVE',
'Uri': 'string'
},
]
}]
}
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP