To meet the personalized query requirements of advanced users, DataService Studio provides a code editor that you can use to customize the SQL statements of APIs. The code editor allows you to use table join queries, complex queries, and aggregate functions. This topic describes how to create an API in the code editor.

Prerequisites

A data source is configured on the Data Source page. For more information, see Configure a data source.

Create an API

  1. Go to the DataService Studio page.
    1. Log on to the DataWorks console.
    2. In the left-side navigation pane, click Workspaces.
    3. In the top navigation bar, select the region in which the workspace that you want to manage resides. Find the workspace and click DataService Studio in the Actions column.
  2. In the Service Development pane, move the pointer over the Create icon icon and choose Create API > Generate API.
    You can also select a business process, right-click API, and then choose Create API > Generate API.
  3. In the Generate API dialog box, set the parameters that are described in the following table.
    Script Mode
    Parameter Description
    API Mode The mode that is used to create the API. Valid values: Wizard Mode and Script Mode. In this example, select Script Mode.
    SQL Mode The SQL mode. Valid values:
    • Basic SQL: Use basic SQL statements to implement the query logic. This mode provides the SQL capability the same as that in earlier versions.
    • Advanced SQL: Use SQL statements with MyBatis tags to implement the query logic. This mode supports the following tag types: if, choose, when, otherwise, trim, foreach, and where.
    API Name The name of the API. The name must be 4 to 50 characters in length, and can contain letters, digits, and underscores (_). The name must start with a letter.
    API Path The path of the API, such as /user.
    Protocol The protocol used by the API. Valid values: HTTP and HTTPS.

    If you want to call the API by using HTTPS, you must bind an independent domain name to the API in the API Gateway console after the API is published to API Gateway. You must also upload an SSL certificate in the API Gateway console. For more information, see Enable HTTPS for an API operation.

    Request Method The request method. Valid values: GET and POST.
    Note If you select GET for the Request Method parameter, you can select only QUERY for the Parameter Position parameter. If you select POST for the Request Method parameter, you can select QUERY or BODY for the Parameter Position parameter.
    Response Content Type The format of the data returned by the API. Set the value to JSON.
    Visible Range The range of users to whom the API is visible. Valid values:
    • Work Space: The API is visible to all members in the current workspace.
    • Private: The API is visible only to its owner, and permissions on the API cannot be granted to other members.
      Note If you set this parameter to Private, other members in the workspace cannot view the API in the API list.
    Label Select tags from the Label drop-down list. For more information, see Create and manage API tags.
    Note A tag can be up to 20 characters in length, and can contain letters, digits, and underscores (_). You can add at most five tags to an API.
    Description The description of the API. The description can be up to 2,000 characters in length.
    Destination Folder The folder that stores the API.
  4. Click Determine.

Configure the API

  1. Double-click the API in the API list. On the tab that appears, set the Datasource Type and Datasource Name parameters in the Select Table section.
    Select Table section
    Note A data source must be selected first, and table join queries are supported only in the same data source.
  2. In the Environment Configuration section, set the Memory and Function Timeout parameters.
    Environment Configuration section
    Note We recommend that you set the Function Timeout parameter to a value equal to or less than 10000 for a shared resource group and 30000 for an exclusive resource group for DataService Studio.
  3. In the Edit query SQL section, enter an SQL statement for querying data.
    • If you set the SQL Mode parameter to Basic SQL, you can enter only a basic SQL statement. Edit query SQL section
      Note The SELECT statement specifies the parameters that the API returns. The WHERE clause specifies the request parameters of the API. You must use ${} to interpolate a request parameter.
      Follow these rules when you enter an SQL statement:
      • Single-table queries, table join queries, and nested queries are supported in the same data source.
      • The SQL statement must meet the following requirements:
        • You can enter only one SQL statement.
        • An SQL comment is not allowed.
        • Only the SELECT statement is supported. Other statements such as INSERT, UPDATE, or DELETE are not supported.
        • The SELECT \* statement is not supported. You must specify the columns to be queried.
        • ${param} cannot be enclosed in single quotation marks (' '). For example, '${id}' or 'abc${xyz}123' is not allowed. If necessary, you can use concat('abc', ${xyz}, '123') instead.
        • Parameters cannot be configured as optional.
      • If the name of a column that the SELECT statement specifies has a table prefix, such as t.name, you must set an alias for the corresponding response parameter. For example, you can specify t.name as name.
      • If you use an aggregate function, such as min, max, sum, or count, you must set an alias for the corresponding response parameter. For example, you can specify sum(num) as total\_num.
      • ${param} in the SQL statement, including ${param} in strings, is regarded as request parameters and replaced. If an escape character (\) is placed before ${param}, ${param} is processed as a common string.
    • If you set the SQL Mode parameter to Advanced SQL, you can enter an SQL statement with MyBatis tags. Mybatis

      The Advanced SQL mode supports the following types of tags: if, choose, when, otherwise, trim, foreach, and where. You can use SQL statements with MyBatis tags to implement complex query logic, such as null parameter checking, cyclic traversal over multiple values, dynamic queries, dynamic sorting, and aggregate analysis. For more information about sample code for common scenarios, see Sample code for the advanced SQL syntax used for creating an API in script mode.

  4. In the right-side navigation pane, click the Request Param tab. In the Request Param panel, set the parameters described in the following table as required.
    If you set the SQL Mode parameter to Advanced SQL, you must manually add all request parameters that are specified in the SQL statement to the parameter list. This ensures that the parameters that are described in the API details are consistent with the parameters that are used.
    Note
    • Before you preview the result, you must configure the information such as the example value, default value, and description for each request parameter of the API.
    • To improve the match efficiency, specify an indexed field as a request parameter.
    Request Param panel
    Parameter Description
    Parameter Name The name of the request parameter. The name can be up to 64 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). The name must start with a letter.
    Parameter Type The data type of the request parameter. Valid values: STRING, INT, LONG, FLOAT, DOUBLE, and BOOLEAN.
    Parameter Position The position of the request parameter. Valid values: QUERY and BODY.
    Note If you set the Parameter Position parameter to BODY for one or more request parameters, you must set the Content-Type parameter to define the format of the request parameters in the request body.
    Valid values of Content-Type:
    • application/json; charset=utf-8: the JSON format.
    • application/xml; charset=utf-8: the XML format.
    • application/x-www-form-urlencoded; charset=utf-8: the FORM format.
    Required Specifies whether the request parameter is required.
    Example Value The example value of the request parameter.
    Default Value The default value of the request parameter.
    Description The description of the request parameter.
  5. In the right-side navigation pane, click the Response Param tab. In the Response Param panel, set the parameters as required.
    If you set the SQL Mode parameter to Advanced SQL, you must manually add all response parameters that are specified in the SQL statement to the parameter list. This ensures that the parameters that are described in the API details are consistent with the parameters that are used. Response Param panel
    Parameter Description
    Parameter Name The name of the response parameter. The name can be up to 64 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). The name must start with a letter.
    Parameter Type The data type of the response parameter. Valid values: STRING, INT, LONG, FLOAT, DOUBLE, and BOOLEAN.
    Example Value The example value of the response parameter.
    Description The description of the response parameter.

    You can select Pagination in the Advanced Settings section.

    Select Pagination based on your needs, and take note of the following items:
    • If you do not select Pagination, the API returns up to 2,000 records.
    • If the API returns more than 2,000 records, we recommend that you select Pagination.
    Note If you select Pagination in the Response Param panel, the number of records to return is subject to the pagination settings. Even if you set a limit for the number of records to return in the Edit query SQL section, this limit does not take effect.
    The following common parameters are displayed when you select Pagination:
    • Common request parameters
      • pageNum: the number of the page to return.
      • pageSize: the number of entries to return on each page.
    • Common response parameters
      • pageNum: the number of the page to return.
      • pageSize: the number of entries returned on each page.
      • totalNum: the total number of entries returned.
    Note Request parameters are optional for an API. If you do not specify request parameters for an API, you must select Pagination.
  6. Configure a filter.
    If you need to process the request parameters of an API or process the results returned by an API, you can configure a filter for the API by performing the following steps: In the right-side navigation pane, click Filter. In the Filter panel, select Use Pre-filter or Use Post-filter based on your needs. After you specify the Function Type parameter, select one or more functions from the drop-down list next to the Use Pre-filter or Use Post-filter check box. If you select multiple functions, the functions are used to process parameters based on the sequence in which they are selected. Then, you can click Preview Responses Returned by API Operation and check whether the processing results of the selected functions meet your expectations. For more information about how to create and use a filter, see Create an Aviator function and use the Aviator function as a filter and Use Python functions as filters.
    Note
    • If you want to use a Python function as a filter, you must activate DataWorks Professional Edition or a more advanced edition and use the shared resource group for DataService Studio.
    • If you want to use an Aviator function as a filter, all DataWorks editions are supported, but you must use an exclusive resource group for DataService Studio.
    • If the function that you want to use is not available in the drop-down list, check whether the function is published. If the function is not published, publish the function. Alternatively, you can create and publish a new function. For more information, see Publish a function.
    Filter panel
  7. Configure a resource group for DataService Studio.
    1. In the right-side navigation pane, click the Resource Group tab. In the panel that appears, select Exclusive Resource Group for DataService Studio or Shared Resource Group for DataService Studio for the Scheme parameter. If you select Exclusive Resource Group for DataService Studio, select an exclusive resource group from the drop-down list. If you select Shared Resource Group for DataService Studio, you do not need to select a shared resource group. DataWorks automatically manages the shared resource group.
      API
      Note If you cannot select an exclusive resource group from the drop-down list, log on to the DataWorks console and associate the exclusive resource group with your workspace.
  8. Click the Save icon icon in the toolbar. After the API is saved, the selected resource group takes effect during the test.
    After the API is configured, you can test it. For more information, see Test an API.

    After the test succeeds, click Submission in the upper-right corner.

    In the right-side navigation pane of the API configuration tab, click Version. Find the API version that you want to publish and click Application for publication in the Actions column to go to the application page. You can use the default application type and enter the reason for the application. Then, submit the application.
    Note If you have defined an approval policy in DataWorks Approval Center, the API must be approved through the approval process before the API can be published. For more information, see Overview.

    After the API is published, the configuration of the resource group for DataService Studio takes effect when the API is called.

    On the Service Development tab, you can find the registered API in the left-side navigation pane and manage the API. For example, you can clone or delete the API. On the Service Management tab, you can find the published API in the API list and view the details of the published API. For more information, see View, delete, move, and clone an API.