ใช้ Boto3 ดึงข้อมูล Function Definition เฉพาะตัวจากฐานข้อมูลใน AWS Glue Data Catalog
ตัวอย่าง
ดึงข้อมูลฟังก์ชันชื่อ insert_employee_record ในฐานข้อมูล employee
ขั้นตอน/อัลกอริทึม
- Import boto3 และ botocore.exceptions.ClientError
- database_name และ function_name เป็นพารามิเตอร์บังคับ
- สร้าง AWS Session และ Glue Client
- เรียก get_user_defined_function(DatabaseName=database_name, FunctionName=function_name)
- คืนค่าข้อมูลฟังก์ชัน (หรือโยน Error ถ้าไม่พบ)
- จัดการ Exception
ตัวอย่างโค้ด
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' }] } }