Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Lấy định nghĩa nhiều hàm người dùng trong Database AWS Glue với Boto3

Dùng boto3 gọi get_user_defined_functions để lấy metadata của nhiều hàm (User Defined Functions) trong database, có thể lọc bằng regex pattern.

Quy trình

  1. Import boto3, ClientError.
  2. Hàm nhận database_name (tùy chọn) và pattern (tùy chọn).
  3. Tạo session, client Glue.
  4. Gọi glue_client.get_user_defined_functions(DatabaseName=..., Pattern=...).
  5. Trả về danh sách hàm.
  6. Xử lý ngoại lệ.

Code mẫu

import boto3
from botocore.exceptions import ClientError

def lay_dinh_nghia_nhieu_ham(database_name=None, pattern=None):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.get_user_defined_functions(DatabaseName=database_name, Pattern=pattern)
        return response
    except ClientError as e:
        raise Exception("Lỗi boto3 client: " + str(e))
    except Exception as e:
        raise Exception("Lỗi không mong đợi: " + str(e))

print(lay_dinh_nghia_nhieu_ham('employee'))

Kết quả

{'UserDefinedFunctions': [{'FunctionName': 'insert_employee_record', 'DatabaseName': 'employee', 'ClassName': 'InsertEmployee', 'OwnerName': 'string', 'OwnerType': 'USER|ROLE|GROUP', 'CreateTime': datetime(2021,3,15), 'ResourceUris': [{'ResourceType': 'JAR|FILE|ARCHIVE', 'Uri': 'string'}]}]}