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

ดึงรายละเอียด Function Definitions หลายตัวจาก AWS Glue Data Catalog ด้วย Boto3

ใช้ Boto3 ดึงข้อมูล User Defined Functions หลายฟังก์ชันจากฐานข้อมูลใน AWS Glue Data Catalog

ปัญหา

ใช้ Boto3 ใน Python ดึงรายละเอียด Function Definitions หลายตัวจาก Database(s) ใน AWS Glue Data Catalog

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

  1. Import boto3 และ botocore.exceptions.ClientError
  2. database_name และ regular_pattern เป็นพารามิเตอร์ทางเลือก (Optional)
  3. สร้าง AWS Session และ Glue Client
  4. เรียก get_user_defined_functions(DatabaseName=database_name, Pattern=regular_pattern)
  5. คืนค่ารายการ Functions ตามเงื่อนไข
  6. จัดการ 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' }] }] }