Create an Aviator function

Updated at:

Create an Aviator function in DataService Studio to process API request parameters or response data as a filter.

Prerequisites

Background

In DataService Studio, you can create, test, and deploy functions. After a function is deployed, you can associate it with an API as a filter to process request parameters or response data, enabling complex query logic for various business scenarios. For code examples, 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 pre-filter and post-filter must use the same function type. Select either Aviator or Python for both.

  • Aviator functions in DataService Studio support all built-in Aviator classes and the additional classes listed below. Classes other than the built-in Aviator classes and those listed below are not currently supported.

    java.util.Collection
    java.util.List
    java.util.Map
    java.util.Set
    java.util.ArrayList
    java.util.LinkedList
    java.util.ArrayDeque
    java.util.HashMap
    java.util.LinkedHashMap
    java.util.TreeMap
    java.util.HashSet
    java.util.LinkedHashSet
    java.util.TreeSet
    java.util.Arrays
    java.util.Collections
    java.lang.String
    java.lang.StringBuilder
    java.lang.StringBuffer
    java.lang.Boolean
    java.lang.Byte
    java.lang.Short
    java.lang.Integer
    java.lang.Long
    java.lang.Float
    java.lang.Double
    java.lang.Character
    java.lang.Math
    java.math.BigDecimal
    java.math.BigInteger
    java.math.RoundingMode
    java.util.Date
    java.util.Calendar
    java.util.GregorianCalendar
    java.text.SimpleDateFormat
    java.sql.Date
    java.sql.Time
    java.sql.Timestamp
    java.time.LocalDate
    java.time.LocalTime
    java.time.LocalDateTime
    java.time.Instant
    java.time.OffsetDateTime
    java.time.OffsetTime
    java.time.ZonedDateTime
    java.time.ZoneId
    java.time.ZoneOffset
    java.time.Duration
    java.time.Period
    java.time.YearMonth
    java.time.format.DateTimeFormatter
    java.time.temporal.ChronoUnit
    java.util.UUID
    java.util.Base64
    java.util.regex.Pattern
    java.net.URLEncoder
    java.net.URLDecoder
    java.lang.Exception
    java.lang.RuntimeException
    java.lang.IllegalArgumentException
    java.lang.IllegalStateException
    java.lang.NumberFormatException
    java.lang.ArithmeticException
    java.lang.NullPointerException
    java.lang.IndexOutOfBoundsException
    java.lang.UnsupportedOperationException
    java.lang.Error
    org.apache.commons.lang3.StringUtils
    org.apache.commons.lang3.time.DateUtils
    org.apache.commons.lang3.time.DateFormatUtils
    org.apache.commons.lang3.math.NumberUtils
    org.apache.commons.codec.binary.Base64
    org.apache.commons.codec.binary.Hex
    org.apache.commons.codec.digest.DigestUtils
    org.apache.commons.codec.digest.HmacUtils
    com.alibaba.fastjson.JSON
    com.alibaba.fastjson.JSONObject
    com.alibaba.fastjson.JSONArray
    com.alibaba.fastjson2.JSON

Usage notes

Aviator functions must run in an exclusive resource group for DataService Studio for security and performance reasons. 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;

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 navigation pane of the API 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 you create the function, test and deploy it for use as an API pre-filter or post-filter.

Next steps