Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ดึงรายละเอียด User-Defined Function เฉพาะตัวจาก AWS Glue Data Catalog ด้วย Boto3

ใช้ Boto3 ดึงข้อมูล Function Definition เฉพาะตัวจากฐานข้อมูลใน AWS Glue Data Catalog

ตัวอย่าง

ดึงข้อมูลฟังก์ชันชื่อ insert_employee_record ในฐานข้อมูล employee

ขั้นตอน/อัลกอริทึม

  1. Import boto3 และ botocore.exceptions.ClientError
  2. database_name และ function_name เป็นพารามิเตอร์บังคับ
  3. สร้าง AWS Session และ Glue Client
  4. เรียก get_user_defined_function(DatabaseName=database_name, FunctionName=function_name)
  5. คืนค่าข้อมูลฟังก์ชัน (หรือโยน Error ถ้าไม่พบ)
  6. จัดการ 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' }] } }