如何使用Boto3从AWS Glue数据目录获取数据库中用户定义函数的详细信息
让我们看看如何从AWS Glue数据目录获取指定函数定义的详细信息。
阅读更多:Python 教程
示例
获取名为 insert_employee_record 的函数定义在数据库 employee 中的详细信息。
问题陈述: 在Python中使用 boto3 库获取AWS Glue数据目录中指定函数定义的详细信息。
解决这个问题的方法/算法
- 步骤1: 导入 boto3 和 botocore 异常处理库来处理异常。
-
步骤2: 需要 database_name 和 function_name 参数。它获取给定 function_name 在给定数据库中的定义。
-
步骤3: 使用 boto3 创建AWS会话。确保在默认配置文件中声明了 ****region_name** ** 。如果没有声明,则创建会话时要显式地传递 region_name 。
-
步骤4: 为 glue 创建一个AWS客户端。
-
步骤5: 调用 get_user_defined_function 并将 database_name 作为DatabaseName参数,将 function_name 作为FunctionName参数传递。
-
步骤6: 它返回给定函数的定义。如果找不到给定函数,则会抛出错误。
-
步骤7: 如果检查函数时发生了错误,则处理通用异常。
示例代码
以下代码获取给定函数的定义-
import boto3
from botocore.exceptions import ClientError
def get_function_definition(database_name, function_name):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_user_defined_function(DatabaseName=database_name, FunctionName=function_name)
return response
except ClientError as e:
raise Exception("boto3 client error in get_function_definition: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_function_definition: " + e.__str__())
a = get_function_definition('employee', 'insert_employee_record')
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'
},
],
}
}
输出结果包含给定函数的详细定义,包括函数名称,所在的数据库名称,类名,所有者名称等等。
极客教程