Updated on 2026-11-06 GMT+08:00

Directly Using a Registered UDAF

After a UDAF has already been registered in the database by another user or script, you can retrieve the registered UDAF operator via udaf.get and call it directly in aggregate without needing to re-register it.

Scenario

In collaborative team development, UDAF registration and usage are often separated. This example demonstrates how to view the list of registered UDFs in the database, retrieve the registered UDAF operator pythontopk, and perform grouping and aggregation on the category_sales table by category, retrieving the top 3 highest amount values for each group.

Notes and Constraints

  • Ensure that the target UDAF is already registered in the specified database before use. Function names are stored in lowercase in the database.
  • The UDAF constructs an aggregation expression using AggregateFnBuilder, with the __init__ parameters passed through constructor_kwargs.

Prerequisites

Example

This example demonstrates how to view the list of registered UDFs in the database, retrieve the registered UDAF operator pythontopk, and perform grouping and aggregation on the category_sales table by product category to get the top 3 records by amount in each group.

  1. Connect to the Aura service and set the UDF workspace.
  2. View the list of registered UDF names in the target database.
  3. If the pythontopk UDAF exists, obtain the UDAF operator.
  4. Load the category_sales dataset.
  5. Construct the aggregation expression using AggregateFnBuilder, grouping by category and taking the top 3 amount values.
  6. Execute the aggregation query and print the results.
  7. Disable the Aura connection.
import os
from aura_frame.multimodal import ai_lake
from aura_frame.multimodal.function import AggregateFnBuilder

target_database = "test"  #Create a database in LakeFormation, for example, test. For details, see Creating a DataArts Lake Formation Instance and Planning Metadata.
#Connect to the AuraJobV2 endpoint. For details about the parameters, see Establishing a Connection with the AuraJobV2 Endpoint.
con = ai_lake.connect(
    aura_endpoint=os.getenv("aura_endpoint"),         # Endpoint where the AuraJobV2 endpoint is located
    aura_endpoint_id=os.getenv("aura_endpoint_id"),    # AuraJobV2 endpoint ID
    aura_endpoint_name=os.getenv("aura_endpoint_name"),# AuraJobV2 endpoint name
    aura_workspace_id=os.getenv("aura_workspace_id"),  # Workspace ID
    lf_catalog_name=os.getenv("lf_catalog_name"),      # LakeFormation Catalog name
    access_key=os.getenv("access_key"),                # Access Key ID (AK)
    secret_key=os.getenv("secret_key"),                # Secret Key (SK)
    default_database=target_database,                       # LakeFormation database name
    use_single_cn_mode=True,   # Whether to enable the single-CN mode
    logging_level=logging.INFO # Set the log level.
)
#Configure the UDF deployment workspace. For details about the parameters, see Configuring the UDF Deployment Workspace.
con.set_function_staging_workspace(
    obs_directory_base=os.getenv("obs_directory_base"),  # Path for storing UDFs in the parallel file system.
    obs_bucket_name=os.getenv("obs_bucket_name"),        # Name of the parallel file system.
    obs_server=os.getenv("obs_server"),                  # OBS endpoint
    access_key=os.getenv("access_key"),                  # Access Key ID (AK)
    secret_key=os.getenv("secret_key"),                  # Secret Key (SK)
)
try:
    # View the names of all registered UDFs.
    udf_names = con._conn.udaf.names(database=target_database)
    print(f"Registered UDFs: {udf_names}")
    if "pythontopk" in udf_names:
        # Retrieve the registered UDAF operator.
        topk_udaf = con._conn.udaf.get(name="pythontopk", database=target_database)
        ds = con.load_dataset("category_sales", database=target_database)
        # Use in aggregate, passing the __init__ parameter via constructor_kwargs.
        udaf_builder = AggregateFnBuilder(
            fn=topk_udaf, on=[ds.amount], as_col="top3",
            constructor_kwargs={"k": 3},
        )
        summary = ds.aggregate([udaf_builder], by=[ds.category])
        df = summary.execute()
        print(df)
finally:
    con.close()