如何使用Boto3从AWS数据目录获取数据库中多个函数定义的详细信息
让我们看看用户如何从AWS Glue数据目录获取多个函数定义的详细信息。
阅读更多:Python 教程
示例
问题陈述: 使用Python中的 boto3 库,从AWS Glue数据目录获取一个或多个数据库中存在的多个函数定义的详细信息。
解决此问题的方法/算法
- 第一步: 导入 boto3 和 botocore 异常以处理异常。
-
第二步: database_name 和 regular_pattern 是可选参数。如果没有提供这些详细信息,函数将获取AWS用户帐户中所有函数的定义。如果提供了 database_name 但未提供 regular_pattern ,那么它将获取给定数据库中的所有函数。如果两个参数都提供,则按照 regular_pattern 匹配函数的定义。如果只提供了 regular_pattern ,则获取与AWS用户帐户中存在的 regular_pattern 匹配的所有函数定义。
-
第三步: 使用 boto3 库创建AWS会话。确保在默认配置文件中提供了 region_name 。如果没有提供,则在创建会话时明确传递 region_name 。
-
第四步: 为 glue 创建AWS客户端。
-
第五步: 调用 get_multiple_function_definition 并将 database_name 作为DatabaseName参数和 regular_pattern 作为Pattern参数传递。
-
第六步: 返回按提供的参数定义的多个函数。
-
第七步: 如果在检查函数时出现错误,请处理通用异常。
示例代码
以下代码获取多个函数的定义:
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'
},
]
}]
}
极客教程