Create an Aviator function

更新时间:
复制 MD 格式

This topic explains how to create an Aviator function.

Prerequisites

Background

In DataService Studio, you can create, test, and deploy functions. After a function is deployed, you can associate it with an API and use it as a filter to process request parameters or response data. This allows you to meet complex query requirements and adapt to various business scenarios. For code examples of common use cases, see Best practices: Use an Aviator function as a filter.

Limitations

  • Aviator functions must run in an exclusive resource group for DataService Studio.

  • When you use a function as an API filter, the function types for the pre-filter and the post-filter must be the same. This means you must select either the Aviator function type or the Python function type for both.

Usage notes

For security and performance reasons, Aviator functions must run in an exclusive resource group for DataService Studio. Therefore:

  • When you edit or test a function, you must associate the function with an exclusive resource group for DataService Studio. For more information, see Manage a function.

  • When you enable a filter and associate it with the target function during API development, you must associate the API with an exclusive resource group for DataService Studio. For more information, see Generate an API from a data source (API Gateway).

Function structure

Aviator functions use the following built-in template. For syntax details, see Aviator function syntax.

## --- AviatorScript ---
## $0: the default name of the input param.
## Expressions must be separated by the semicolon ";".
## Example 1:
## $0.a: Get the attribute "a" from the input param "$0".
## $0.b[index]: Get the element at the specified index from the array "b" in the input param "$0". The index cannot be an expression or a variable.
## Example 2:
## Input: {"keyA":[1,2],"keyB":"value"}
## Function: $0.keyA[0]=3; $0.keyB="new_value"; return $0;
## Output: {"keyA":[3,2],"keyB":"new_value"}
return $0;

You can modify the function based on this template.

  • The default input parameter name for an Aviator function is $0, and it cannot be changed. You can use $0 to obtain the entire input of the function.

  • In DataService Studio, functions serve as API filters to process request parameters and response data. The function input, represented by $0, is therefore a JSON object. Request parameters are passed as a flat JSON structure (depth 1), and response data as a nested JSON structure (depth N).

  • When a function is used as an API pre-filter, the system converts the API request parameters or the output from a previous function from a key-value map into a JSON object and passes it to the $0 parameter. The value for each key in the JSON object must be a string.

  • When the function is used as an API pre-filter, its output must be a key-value map. This output serves as the input parameters for SQL statement execution. Therefore, only a flat key-value map with a depth of one is supported.

Go to DataService Studio

Log on to the DataWorks console. In the target region, click Data Analysis and Service > DataService Studio in the left-side navigation pane. Select a workspace from the drop-down list and click Go to DataService Studio.

Create a function

  1. On the Service Development page, hover over the image.png icon and click Create Function > Create Aviator Function.

    Alternatively, find the target workflow, right-click Function, and choose Create Function > Create Aviator Function.

  2. In the Create Aviator Function dialog box, configure the parameters.

    Parameter

    Description

    Function Name

    A unique name for the function, up to 256 characters in length.

    Function Template

    Only AviatorScript Standard v1 is supported.

    Function Description

    A brief description of the function, up to 512 characters in length.

    Location

    The destination directory for the function.

    Note

    Select a specific workflow, folder, or function category. If no workflow exists, create one first.

  3. Click Determine.

  4. In the Edit Code area, enter the code for the function.

  5. Configure the resource group.

    1. In the right-side pane of the function editing page, click Resource Group for DataService Studio. In the Resource Group Type section, configure the resource group type for API calls.

      You can only select an exclusive resource group for DataService Studio.

      Note

      If you cannot select the target resource group name from the list, go to the DataWorks console and bind the resource group to your workspace using Modify Workspace Ownership. You can also click Purchase Exclusive Resource Group for DataService Studio to purchase a new one. For more information, see Use an exclusive resource group for DataService Studio.

    2. In the Environment Configuration section, you can set the Memory and Timeout.

      The recommended function timeout is 100 ms or less for an exclusive resource group for DataService Studio.

  6. Click the 保存 icon in the toolbar to save the function.

After creating the function, you can test and deploy it for use as an API pre-filter or post-filter.

Next steps