ใช้ Boto3 ดึงข้อมูล User Defined Functions หลายฟังก์ชันจากฐานข้อมูลใน AWS Glue Data Catalog
ปัญหา
ใช้ Boto3 ใน Python ดึงรายละเอียด Function Definitions หลายตัวจาก Database(s) ใน AWS Glue Data Catalog
ขั้นตอน/อัลกอริทึม
- Import boto3 และ botocore.exceptions.ClientError
- database_name และ regular_pattern เป็นพารามิเตอร์ทางเลือก (Optional)
- สร้าง AWS Session และ Glue Client
- เรียก get_user_defined_functions(DatabaseName=database_name, Pattern=regular_pattern)
- คืนค่ารายการ Functions ตามเงื่อนไข
- จัดการ Exception
ตัวอย่างโค้ด
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' }] }] }