All Products
Search
Document Center

DataWorks:Create an Aviator function

Last Updated:Nov 13, 2024

This topic describes how to create an Aviator function.

Prerequisites

Background information

On the DataService Studio page, you can create, test, and publish a function. Then, you can associate the function with an API, and use the function as a filter to process the request parameters or responses of the API to perform complex queries based on your business requirements. For more information about the sample code that shows how to use an Aviator function as a filter for an API in common scenarios, see Best practices of using Aviator functions as filters.

Limits

  • You must use an exclusive resource group for DataService Studio to run an Aviator function.

  • For the same API, the function used as the prefilter and the function used as the post-filter must be of the same type.

Precautions

To ensure data security and function performance, you must use an exclusive resource group for DataService Studio to run an Aviator function.

  • 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 functions.

  • If you enable the filter feature for an API and associate the API with a function when you develop the API, you must associate the API with an exclusive resource group for DataService Studio. For more information, see Create an API by using the codeless UI.

Syntax of an Aviator function

The system provides the following function template for an Aviator function. For more information about the syntax of an Aviator function, 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 index of the array "b" from 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 your business requirements.

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

  • The input of a function must be in the JSON format. In DataService Studio, a function is used as a filter to process the request parameters or responses of an API. The request parameters of an API are defined in the single-layer JSON format. The responses of the API are defined in the multi-layer JSON format. Therefore, the input of a function must also be in the JSON format. The $0 parameter specifies input data in the JSON format.

  • If a function is used as the prefilter for an API, the system converts the request parameters of the API or outputs of the previous function from key-value pairs into data in the JSON format. Then, the system uses the data in the JSON format to replace the $0 parameter. The data type of values in the JSON data can only be STRING.

  • If a function is used as the prefilter for an API, the outputs of the function are key-value pairs. The key-value pairs are used as the input parameter of an SQL statement. Therefore, the key-value pairs cannot contain nested key-value pairs.

Go to the DataService Studio page

Log on to the DataWorks console. In the top navigation bar, select the desired region. Then, click DataService Studio in the left-side navigation pane. On the page that appears, select the desired workspace from the drop-down list and click Go to DataService Studio.

Create a function

  1. In the Service Development pane, move the pointer over the image.png icon, and then choose Create Function > Create Aviator Function.

    Alternatively, you can find the desired business process, right-click Function, and then choose Create Function > Create Aviator Function.

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

    Parameter

    Description

    Function Name

    The name of the function. The name can be up to 256 characters in length.

    Function Template

    The template that is used to create the function. Set the value to AviatorScript Standard v1.

    Function Description

    The description of the function. The description can be up to 512 characters in length.

    Destination Folder

    The folder for storing the function.

    Note

    The value of this parameter can be a business process name, folder name, or function category. If no business process is available, you must create a business process.

  3. Click Determine.

  4. In the Edit Code section, enter the function code.

  5. Configure a resource group for DataService Studio.

    函数

    1. In the right-side navigation pane of the configuration tab of the function, click Resource Group. In the Resource Group Type section of the Resource Group tab, select Exclusive Resource Group for DataService Studio for the Scheme parameter and select a resource group from the Exclusive Resource Group for DataService Studio drop-down list.

      You can select only Exclusive Resource Group for DataService Studio for the Scheme parameter.

      Note

      If the exclusive resource group for DataService Studio that you want to select is not displayed in the drop-down list, you can go to the Resource Groups page of the DataWorks console, and then associate the exclusive resource group with your workspace on the Exclusive Resource Groups tab. You can also click Purchase Resource Group for DataService Studio on the Resource Group tab to purchase an exclusive resource group for DataService Studio. For more information, see Create and use an exclusive resource group for DataService Studio.

    2. In the Environment Configuration section, configure the Memory and Function Timeout parameters.

      We recommend that you set the Function Timeout parameter to a value that is less than or equal to 1000 for the shared resource group and less than or equal to 100 for an exclusive resource group for DataService Studio.

  6. Click the 保存 icon in the toolbar.

After the function is created, you can test and publish the function. Then, you can associate the function with an API and use the function as a prefilter or post-filter for the API.

What to do next

  • After you create the function, you can test and publish the function. For more information, see Test a function and Publish a function.

  • After you publish the function, you can associate the function with an API and use the function as a prefilter or a post-filter for the API. This way, the function can process the request parameters or responses of the API. For more information, see Use a function as a filter for an API.