如何使用 Boto3 从 AWS 数据目录中获取数据库中多个函数定义的详细信息
让我们看看用户如何从AWSGlue数据目录获取多个函数定义的详细信息。
示例
问题陈述:使用Python中的boto3库database(s)从AWSGlue数据目录中获取存在于a中的多个函数定义的详细信息。
解决这个问题的方法/算法
第一步:导入boto3和botocore异常处理异常。
第二步:database_name和regular_pattern是可选参数。如果未提供这些详细信息,该函数将获取AWS用户账户中存在的所有函数的定义。如果提供了database_name但未提供regular_pattern,则它会获取给定数据库中的所有函数。如果提供了这两个参数,那么它会根据regular_pattern获取匹配函数的定义。如果只提供了regular_pattern,它将获取与AWS用户帐户中存在的regular_pattern匹配的所有函数
步骤3:使用boto3lib创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name。
第4步:为glue创建一个AWS客户端。
步骤5:调用get_multiple_function_definition并将database_name作为DatabaseName和regular_pattern作为Pattern参数传递。
步骤6:它根据提供的参数返回多个函数的定义。
第7步:如果在检查函数时出现问题,则处理通用异常。
示例代码
以下代码获取多个函数的定义-
import boto3 frombotocore.exceptionsimport 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' }, ] }] }