DataService Studio lets you create APIs in either Wizard Mode or Script Mode. Script Mode offers more flexibility than Wizard Mode, letting you write custom SQL queries for complex operations such as multi-table joins, advanced queries, and aggregate functions.
Prerequisites
-
Before you configure an API, you must configure a data source on the page. For more information, see Configure a data source.
-
Prepare a resource group for DataService Studio. We recommend that you use a serverless resource group in a production environment. For more information, see Resource groups and network connectivity.
DataService Studio
Log on to the DataWorks console. In the target region, click in the left-side navigation pane. Select a workspace from the drop-down list and click Go to DataService Studio.
Generate an API
-
On the Service Development page, hover over the
icon, and click .Alternatively, open a business process, right-click API, and select .
-
In the Generate API dialog box, configure the parameters.
Parameter
Description
API Mode
The options are Wizard Mode and Code Editor. Select Code Editor.
SQL Mode
The options are Basic SQL and Advanced SQL.
-
Basic SQL: Write query logic by using basic SQL syntax. The syntax is compatible with earlier versions of SQL.
-
Advanced SQL: Write query logic by using SQL syntax that supports MyBatis tags. The supported tags are:
if,choose,when,otherwise,trim,foreach, andwhere.
API Name
The name must be 4 to 50 characters, contain only Chinese characters, letters, digits, and underscores (_), and start with a Chinese character or a letter.
APIPath
The path where the API is stored. For example, /user.
Protocol
The supported protocols are HTTP and HTTPS.
To call the API over HTTPS, publish it to API Gateway, then bind a custom domain name and upload an SSL certificate in the API Gateway console. For more information, see Enable HTTPS.
Request Method
The supported methods are GET and POST.
NoteIf Request Method is GET, Parameter location can only be QUERY. If Request method is POST, Parameter location can be Body or Body.
Response Type
Only the JSON format is supported.
Visible Scope
The options are Work space and Private.
-
Work space: The API is visible to all members of the workspace.
-
Private: The API is visible only to its owner, who cannot grant permissions to other members.
NoteIf you set the visible scope to Private, only you can see the API in the directory tree.
Tag
Select a tag from the Tag list. For more information, see Create and manage API tags.
NoteA tag name can contain Chinese characters, letters, digits, and underscores (_). You can add up to five tags, and each tag can be up to 20 characters long.
Description
Provide a brief description of the API. The description can be up to 2,000 characters.
Location
The directory where the API is stored.
-
-
Click Determine.
Configure the API
1. Select a table
Double-click the API to open its editing page. In the Table section, configure parameters such as Data Source Type, Data Source Name, and Data Source Environment.
The required parameters depend on the data source type. See the UI for details.
-
You must configure a data source in . You can search for a table by its name in the data table drop-down list.
-
You must select a data source. Join queries are supported only for tables within the same data source.
-
For a standard-mode workspace, you can select a development or production data source for the Data Source Environment parameter. For more information, see Differences between basic mode and standard mode workspaces.
-
For tables in MaxCompute data sources, you can use the Acceleration Service of DataService Studio or the MCQA service of MaxCompute to accelerate queries. To use the Acceleration Service, you must first create an acceleration item. For more information, see Acceleration Service.
2. Write SQL query
In the Edit Query SQL section, enter the SQL query statement.
-
If you select the Basic SQL mode, only basic SQL syntax is supported.
SELECT name, addr AS address, SUM(num) as total_num FROM table_name WHERE user_id =${uid}NoteFields in the SELECT clause define the response parameters of the API. Parameters in the WHERE clause define the request parameters of the API. Use the
${}format to identify request parameters.Follow these rules when writing the SQL statement:
-
Single-table queries, multi-table join queries, and nested queries within the same data source are supported.
-
The following are not supported:
-
Multiple SQL statements.
-
Comments.
-
Non-SELECT statements, such as INSERT, UPDATE, and DELETE.
-
The
SELECT *statement. You must explicitly specify the columns to query. -
Do not enclose the ${param} variable in quotation marks. For example,
'${id}'and'abc${xyz}123'are not supported. To do this, use theconcat('abc', ${xyz}, '123')function. -
Optional parameters.
-
-
If a column name in the SELECT clause is prefixed with a table name (for example, t.name), you must specify an alias for the response parameter (for example, t.name as name).
-
If you use an aggregate function (such as min, max, sum, or count), you must specify an alias for the response parameter. For example,
sum(num) as total_num. -
The ${param} variables in an SQL statement, including those in strings, are replaced with request parameters. When a ${param} variable is preceded by a backslash (\), it is treated as a literal string.
-
-
If you select the Advanced SQL mode, MyBatis tags are supported.
SELECT id, name, create_name FROM t_parameter <where> <if test="'list!=null'"> id in ( <foreach collection="list" open="(" close=")" separator="," item="id_num"> ${id_num} </foreach> ) </if> </where>The supported MyBatis tags in Advanced SQL are if, choose, when, otherwise, trim, foreach, and where. You can use these tags to implement complex query logic, such as null value checks, multi-value traversals, dynamic table queries, dynamic sorting, and aggregations. For code samples in common scenarios, see Advanced SQL (MyBatis syntax) examples.
When using special characters in MyBatis tags, you must escape these characters. The following table lists common escape characters.
Special character
Escape character
Description
>
>
Greater than
>=
>=
Greater than or equal to
<
<
Less than
<=
<=
Less than or equal to
&
&
And
'
'
Single quotation mark
"
"
Double quotation mark
3. Configure request parameters
In the right-side pane of the API editing page, click Request Parameters and configure the parameters.
If you use the Advanced SQL mode, manually add all request parameters from the SQL script to the list. This ensures the API documentation accurately reflects the required parameters.
-
Before you preview the results, set the example value, default value, and description for the API parameters.
-
For better performance, set indexed fields as request parameters.
|
Parameter |
Description |
|
Parameter Name |
The name of the request parameter. The name can be up to 64 characters in length, must start with a letter, and can contain letters, digits, underscores (_), and hyphens (-). |
|
Parameter Type |
Supported data types are STRING, INT, LONG, FLOAT, Double, and Boolean. |
|
Parameter Position |
The options are QUERY and Body. Note
When you select Body as the location for one or more parameters, you must also set the Content-Type for the parameters in the Body to define the format for passing parameters in the request body. The supported Content-Type values are:
|
|
Required |
Specifies whether the request parameter is required. |
|
Sample Value |
An example value for the request parameter. |
|
Default Value |
The default value of the request parameter. |
|
Description |
A brief description of the request parameter. |
4. Configure response parameters
In the right-side pane of the API editing page, click Response Parameters and configure the parameters.
-
Configure the response parameters.
If you use the Advanced SQL mode, manually add all response parameters from the SQL script to the list. This ensures the API documentation accurately reflects the returned data.
Parameter
Description
Parameter Name
The name of the response parameter. The name can be up to 64 characters in length, must start with a letter, and can contain letters, digits, underscores (_), and hyphens (-).
Parameter Type
Supported data types are STRING, INT, LONG, FLOAT, Double, and Boolean.
Sample Value
An example value for the response parameter.
Description
A brief description of the response parameter.
-
In the Advanced Settings section, specify whether to enable Pagination for Return Results.
-
If you do not enable Pagination for Return Results, the API returns a maximum of 2,000 records by default.
-
Enable Pagination for Return Results if the API might return more than 2,000 results. After it is enabled, you can go to the Resource Group for DataService Studio page in the right-side navigation pane to set the maximum number of entries per page based on the resource group type.
NoteIf Pagination for Return Results is enabled on the Response Parameters tab of the API editing page, and you use a SQL statement with a
LIMITclause in the Edit Query SQL section, theLIMITclause is ignored, and the number of returned results is determined by the Pagination for Return Results settings.After you enable pagination, the following common parameters are automatically added:
-
Common request parameters
-
returnTotalNum: determines whether to return the total number of data records in a single request.
-
pageNum: the current page number.
-
pageSize: the number of entries per page.
-
-
Common response parameters
-
pageNum: the current page number.
-
pageSize: the number of entries per page.
-
totalNum: the total number of records.
-
NoteAn API can have no request parameters, but in that case, you must enable Pagination for Return Results.
-
5. Configure filters
If you need to preprocess the request parameters or post-process the query results, click Filter in the right-side navigation pane of the API editing page. Select Use Pre-filter or Use Post-filter as needed. Select a Function Type, and then select one or more functions from the pre-filter or post-filter drop-down list. Functions are executed in the order they are added. After the configuration is complete, you can click Preview Responses Returned by API Operation to check whether the results meet your expectations. For more information about how to create and use filters, see Create an Aviator function and Create a Python function.
-
Using a Python function as a filter requires DataWorks Professional Edition or a higher edition and a Shared Resource Group for DataService Studio.
-
Using an Aviator function as a filter does not require a specific DataWorks edition, but it does require an Exclusive Resource Group for DataService Studio.
-
If the target function is not found in the filter drop-down list, check whether the function is published, or try to create and publish a new function. For more information, see Publish a function.
6. Configure service resource group
-
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 type of resource group to use when calling the API.
You can select Exclusive Resource Group for DataService Studio or Shared Resource Group for DataService Studio. For an Exclusive Resource Group for DataService Studio, you can select a target resource group name from the list. A Shared Resource Group for DataService Studio is automatically managed by DataWorks and cannot be selected by name.
Note-
If the target resource group name is not in the list, go to the Resource Groups page, find the resource group, and click Associate Workspace in the Operation column.
-
If the target resource group is in the list but cannot be selected, go to the Resource Groups page, find the resource group, and in the Operation column, click . Manually set the Occupied CUs (for pay-as-you-go resource groups) or Minimum CUs (for subscription resource groups) for the Data Services purpose.
-
-
In the Environment Configuration area, you can set Memory, Timeout, and Maximum Number of Data Records for a Single Request.
-
The maximum timeout depends on the type of DataWorks service resource group and API Gateway instance selected:
-
shared API Gateway instance: up to 30,000 ms for a Shared Resource Group for DataService Studio, and up to 30,000 ms for an Exclusive Resource Group for DataService Studio.
-
dedicated API Gateway instance: up to 30,000 ms for a Shared Resource Group for DataService Studio, and up to 90,000 ms for an Exclusive Resource Group for DataService Studio.
NoteAn API's response time depends on its SQL execution time. To prevent request failures, set the API Timeout to a value greater than the expected execution time.
-
-
The maximum entries per page depends on the type of service resource group selected:
-
If you select a Shared Resource Group for DataService Studio, the maximum number of data records per page with pagination enabled is 2,000.
-
If you select an Exclusive Resource Group for DataService Studio, the maximum number of data records per page with pagination enabled is 10,000.
NoteThere is no upper limit on the total number of results an API can return.
-
-
7. Save and submit
Click the
Save icon in the toolbar. After you save the API, the selected resource group is used for testing.
Next steps
Test and publish:
After configuring the API, you can test it. For more information, see Test an API.
After the test succeeds, click Submission in the upper-right corner.
On the API editing page, click Version in the right-side navigation pane. Find the version you want to publish and click Request to Publish. On the application page, the application type defaults to Publish API in DataService Studio. Enter a Application Reason and click Requested Permissions to submit your request.
NoteIf your workspace in the DataWorks Approval Center is configured with an approval workflow, the request must be approved before the API can be published. For more information, see Approval Center Overview.
After the API is published, the settings for the resource group for DataService Studio apply to all API calls.
Manage APIs: On the Service Development page, you can manage APIs by cloning or deleting them from the directory tree. On the Service Management page, you can expand the API list to view details of published APIs. For more information, see View, delete, move, clone, perform batch operations on, and search APIs by code.
> Manage Quota