The Data Integration RestAPI Reader plugin enables you to read data from RESTful APIs. By configuring an HTTP request URL, you can retrieve data using various methods, including fetching data within a specific time range, retrieving paginated data, or looping through request parameters. The plugin converts the retrieved data into data types supported by Data Integration and passes it to a downstream writer plugin. This topic describes common use cases for RESTful API data sources.
-
This topic describes the best practices for RestAPI Reader. For an explanation of the parameters used in the examples, see RestAPI (HTTP) data source.
-
To configure a RestAPI Writer script, see RestAPI (HTTP) data source and Writer script parameters.
Background
This topic describes how the DataWorks Data Integration RestAPI Reader reads data and returns results.
|
Property |
Description |
|
Response format |
Only JSON responses are supported. |
|
Readable data types |
You can read the INT, BOOLEAN, DATE, DOUBLE, FLOAT, LONG, and STRING data types. |
|
Request methods |
The RestAPI Reader supports GET and POST request methods. |
|
Authentication methods |
The RestAPI Reader supports no authentication, or one of the following authentication methods: Basic Auth, Token Auth, and Aliyun API Signature. You can select an authentication method that is supported by your data source and configure the required authentication parameters.
|
Practice 1: Read data from an API that queries data within a time range
Sample scenario: API definition
In this practice, you read data from a RESTful API and write it to a MaxCompute partitioned table. The sample RESTful API is a self-built test GET API that returns data within a specified time range based on the input time range parameters. The API details are as follows.
When you perform the actual operations, adjust the configurations based on the API you use. The API in this example is only used as a demonstration to walk you through the workflow.
-
Sample API request:
http://TestAPIAddress:Port/rest/test2?startTime=<StartTime>&endTime=<EndTime>The startTime and endTime parameters specify the time range for reading data.
-
Sample response:
{ "status": "success", "totalNum": 187, "data": [ { "axis": "series1", "value": 9191352, "createTime": "2023-01-04 00:07:20" }, { "axis": "series1", "value": 6645322, "createTime": "2023-01-04 00:14:47" }, { "axis": "series1", "value": 2078369, "createTime": "2023-01-04 00:22:13" }, { "axis": "series1", "value": 7325410, "createTime": "2023-01-04 00:29:30" }, { "axis": "series1", "value": 7448456, "createTime": "2023-01-04 00:37:04" }, { "axis": "series1", "value": 5808077, "createTime": "2023-01-04 00:44:30" }, { "axis": "series1", "value": 5625821, "createTime": "2023-01-04 00:52:06" } ] }The data field is the JSON path for data storage. The returned data contains three columns:
axis,value, andcreateTime. -
Sample API call in a testing tool: Send a GET request in an API testing tool such as Postman. Pass in the startTime and endTime parameters (for example, 2023-01-04 00:00:00 to 2023-01-04 23:59:59). The API returns a 200 OK response with totalNum of 187. The data array contains data within the specified time range.
Preparation: Create a MaxCompute partitioned table
In this practice, data read from the API is synchronized to a MaxCompute partitioned table. You must first create a partitioned table to store the synchronized data.
When a partitioned table is used with the overwrite command, the partition overwrite effect is achieved. This makes the synchronization task rerunnable without data duplication, and partitioned tables are also easier to use for data analysis.
The CREATE TABLE statement is as follows.
CREATE TABLE IF NOT EXISTS ods_xiaobo_rest2
(
`axis` STRING
,`value` BIGINT
,`createTime` STRING
)
PARTITIONED BY
(
ds STRING
)
LIFECYCLE 3650;
If you use DataWorks Standard Edition and submit the created partitioned table to the production environment, you can view the table in Data Map.
Configure the synchronization task
-
Add a RestAPI data source.
Add a RestAPI data source in your DataWorks workspace. For more information, see Add a RestAPI data source. In the Add RestAPI Data Source dialog, specify Data Source Name and Data Source Description, select Applicable Environment (development or production), and configure Default Request Header (default value:
{}). The key configurations are as follows.-
url: Set this to the RESTful API address.
-
Authentication Method: Select an authentication method supported by your data source API and configure the required authentication parameters.
-
Resource group connectivity: RestAPI data sources support only exclusive resource groups for Data Integration. Select an exclusive resource group for Data Integration and test the connectivity between the data source and the resource group.
-
-
Create a batch synchronization node and configure the synchronization task.
Create a batch synchronization node in DataWorks Data Studio. For more information, see Create a batch synchronization node. The key configurations are as follows.
-
Data Source configurations:
-
Data Source: Select the RestAPI data source created in the previous step.
-
Request Method: The sample API is a GET API. Select GET.
-
Return Data Structure: The response of the sample API is a JSON array. Select Array Data.
-
The JSON path for data storage: The data returned by the sample API is stored under the data field. Set this to data.
-
Request Param: Use request parameters together with scheduling parameters to synchronize data for the current day on a daily basis.
-
Set the request parameters to
startTime=${extract_day} ${start_time}&endTime=${extract_day} ${end_time} -
In the schedule settings, add three scheduling parameters:
extract_day=${yyyy-mm-dd},start_time=00:00:00, andend_time=23:59:59.
If the run date is 2023-01-05, the value of
extract_dayis 2023-01-04, and the request parameters are concatenated as:startTime=2023-01-04 00:00:00&endTime=2023-01-04 23:59:59. -
-
-
Data Destination configurations:
-
Data Source and Table: Select the MaxCompute partitioned table created in the previous step.
-
Partition Information: Use partition information together with scheduling parameters.
-
Set the partition information to
${bizdate}. -
In the schedule settings, add one scheduling parameter:
bizdate=$bizdate.
If the run date is 2023-01-05, the partition information value is 20230104.
-
-
-
Field Mapping configurations: Based on the data definition in the API, enter the column names from the RestAPI response. Column names are case-sensitive. After you add the columns, use The same name mapping or manually draw lines to create column mappings.
-
Test run
This practice uses scheduling parameters. After you complete the batch synchronization task configuration, click Run with Parameters in the toolbar at the top of the batch synchronization node page, enter the test scheduling parameter values as prompted, and run the synchronization task for testing. This button is displayed as a run icon with a parameter marker to the right of the Run button. After the test run is complete, you can view the runtime log at the bottom of the page to verify whether the scheduling parameter values meet your expectations.
Data verification
You can run an ad hoc query in Data Studio to verify whether the data is correctly synchronized to MaxCompute. The following is a sample query statement.
select * from ods_xiaobo_rest2 where ds='20230104' order by createtime;
In the statement, ods_xiaobo_rest2 is the MaxCompute partitioned table created in the previous step, and 20230104 is the partition value used during the test run.
After the query is complete, check the query results at the bottom of the page to verify whether the data is correctly synchronized to MaxCompute. The query results table should display the column values for axis, value, and createTime, as well as the partition ds value, which should be consistent with the JSON data returned by the API to confirm that data is correctly synchronized.
Submit, deploy, and backfill data
After the test run and data verification are complete, you can submit and deploy the batch synchronization task to the production environment. For more information, see Deploy a node. Click the Submit icon in the toolbar at the top of the node editing page to complete the submission. After the deployment is successful, you can find this scheduled task in Operation Center. You can then use the backfill data feature to backfill historical data for past time periods. For more information about the backfill data feature and related operations, see Backfill data.
Practice 2: Read data from a paginated RestAPI
Sample scenario: API definition
In this practice, you read data from a RESTful API and write it to a MaxCompute partitioned table. The sample RESTful API is a self-built test GET API. The API details are as follows.
When you perform the actual operations, adjust the configurations based on the API you use. The API in this example is only used as a demonstration to walk you through the workflow.
-
Sample API request:
http://TestAPIAddress:Port/rest/test1?pageSize=5&pageNum=1The pageSize and pageNum parameters specify the page size and page number.
-
Sample response:
{ "status": "success", "totalNum": 304, "data": [ { "id": 6, "name": "Test User 6" }, { "id": 7, "name": "Test User 7" }, { "id": 8, "name": "Test User 8" }, { "id": 9, "name": "Test User 9" }, { "id": 10, "name": "Test User 10" } ] }The data field is the JSON path for data storage. The returned data contains two columns:
idandname. -
Sample API call in a testing tool: Send a GET request in an API testing tool such as Postman. Pass in the parameters pageSize=5 and pageNum=2. The API returns a 200 OK response with totalNum of 304. The data array contains the user data on page 2 (for example, id:6, name:Test User 6).
Preparation: Create a MaxCompute partitioned table
In this practice, data read from the API is synchronized to a MaxCompute partitioned table. You must first create a partitioned table to store the synchronized data.
When a partitioned table is used with the overwrite command, the partition overwrite effect is achieved. This makes the synchronization task rerunnable without data duplication, and partitioned tables are also easier to use for data analysis.
The CREATE TABLE statement is as follows.
CREATE TABLE IF NOT EXISTS ods_xiaobo_rest1
(
`id` BIGINT
,`name` STRING
)
PARTITIONED BY
(
ds STRING
)
LIFECYCLE 3650;
If you use DataWorks Standard Edition and submit the created partitioned table to the production environment, you can view the table in Data Map.
Configure the synchronization task
-
Add a RestAPI data source.
Add a RestAPI data source in your DataWorks workspace. For more information, see Add a RestAPI data source. In the Add RestAPI Data Source dialog, specify Data Source Name and Data Source Description, select Applicable Environment (development or production), and configure Default Request Header (default value:
{}). The key configurations are as follows.-
url: Set this to the RESTful API address.
-
Authentication Method: Select an authentication method supported by your data source API and configure the required authentication parameters.
-
Resource group connectivity: RestAPI data sources support only exclusive resource groups for Data Integration. Select an exclusive resource group for Data Integration and test the connectivity between the data source and the resource group.
-
-
Create a batch synchronization node and configure the synchronization task.
Create a batch synchronization node in DataWorks Data Studio. For more information, see Create a batch synchronization node. The key configurations are as follows.
-
Data Source configurations:
-
Data Source: Select the RestAPI data source created in the previous step.
-
Request Method: The sample API is a GET API. Select GET.
-
Return Data Structure: The response of the sample API is a JSON array. Select Array Data.
-
The JSON path for data storage: The data returned by the sample API is stored under the data field. Set this to data.
-
Request Param: The page size is fixed. Set this to
pageSize=50. We recommend that you do not set a large page size, as it may put excessive pressure on the RestAPI server and the synchronization task. -
The number of requests: Select Multiple Requests for this example.
The pagination parameter for this API is pageNum. After you select Multiple Requests, configure the related parameters as follows:
-
Parameter used for multiple requests: Set this to pageNum.
-
StartIndex: Set this to 1.
-
Step: Set this to 1.
-
EndIndex: Set this to 100.
-
-
-
Data Destination configurations:
-
Data Source and Table: Select the MaxCompute partitioned table created in the previous step.
-
Partition Information: Use partition information together with scheduling parameters.
-
Set the partition information to
${bizdate}. -
In the schedule settings, add one scheduling parameter:
bizdate=$bizdate.
If the run date is 2023-01-05, the partition information value is 20230104.
-
-
-
Field Mapping configurations: Based on the data definition in the API, enter the column names from the RestAPI response. Column names are case-sensitive. After you add the columns, use The same name mapping or manually draw lines to create column mappings.
-
Test run
This practice uses scheduling parameters. After you complete the batch synchronization task configuration, click Run with Parameters in the toolbar at the top of the batch synchronization node page, enter the test scheduling parameter values as prompted, and run the synchronization task for testing. This button is displayed as a run icon with a parameter marker to the right of the Run button. After the test run is complete, you can view the runtime log at the bottom of the page to verify whether the scheduling parameter values meet your expectations.
Data verification
You can run an ad hoc query in Data Studio to verify whether the data is correctly synchronized to MaxCompute. The following is a sample query statement.
select * from ods_xiaobo_rest1 where ds='20230104' order by id;
In the statement, ods_xiaobo_rest1 is the MaxCompute partitioned table created in the previous step, and 20230104 is the partition value used during the test run.
After the query is complete, check the query results at the bottom of the page to verify whether the data is correctly synchronized to MaxCompute. The query results table should display the column values for id and name, as well as the partition ds value, which should be consistent with the JSON data returned by the API to confirm that data is correctly synchronized.
Practice 3: Read data from a POST-type RestAPI
Sample scenario: API definition
In this practice, you read data from a RESTful API and write it to a MaxCompute partitioned table. The sample RESTful API is a self-built test POST API. The API details are as follows.
When you perform the actual operations, adjust the configurations based on the API you use. The API in this example is only used as a demonstration to walk you through the workflow.
-
Sample API request:
http://TestAPIAddress:Port/rest/test3The request body format is JSON.
{ "userId":16, "startTime":"2023-01-04 00:00:00", "endTime":"2023-01-04 23:59:59" } -
Sample response:
{ "status": "success", "totalNum": 289, "data": [ { "user": { "id": 16, "name": "User 16" }, "axis": "series1", "value": 8231053, "createTime": "2023-01-04 00:04:57" }, { "user": { "id": 16, "name": "User 16" }, "axis": "series1", "value": 6519928, "createTime": "2023-01-04 00:09:51" }, { "user": { "id": 16, "name": "User 16" }, "axis": "series1", "value": 2915920, "createTime": "2023-01-04 00:14:36" }, { "user": { "id": 16, "name": "User 16" }, "axis": "series1", "value": 7971851, "createTime": "2023-01-04 00:19:51" }, { "user": { "id": 16, "name": "User 16" }, "axis": "series1", "value": 6598996, "createTime": "2023-01-04 00:24:30" } ] }The data field is the JSON path for data storage. The returned data contains five columns:
user.id,user.name,axis,value, andcreateTime. -
Sample API call in a testing tool: Send a POST request in an API testing tool such as Postman. Set Body to raw format with the JSON type, and pass in a request body that contains userId, startTime, and endTime. The API returns a 200 OK response with totalNum of 289. The data array contains the data for the specified user within the specified time range.
Preparation: Create a MaxCompute partitioned table
In this practice, data read from the API is synchronized to a MaxCompute partitioned table. You must first create a partitioned table to store the synchronized data.
When a partitioned table is used with the overwrite command, the partition overwrite effect is achieved. This makes the synchronization task rerunnable without data duplication, and partitioned tables are also easier to use for data analysis.
The CREATE TABLE statement is as follows.
CREATE TABLE IF NOT EXISTS ods_xiaobo_rest3
(
`user_id` BIGINT
,`name` STRING
,`axis` STRING
,`value` BIGINT
,`create_time` STRING
)
PARTITIONED BY
(
ds STRING
)
LIFECYCLE 3650;
If you use DataWorks Standard Edition and submit the created partitioned table to the production environment, you can view the table in Data Map.
Configure the synchronization task
-
Add a RestAPI data source.
Add a RestAPI data source in your DataWorks workspace. For more information, see Add a RestAPI data source. In the Add RestAPI Data Source dialog, specify Data Source Name and Data Source Description, select Applicable Environment (development or production), and configure Default Request Header (default value:
{}). The key configurations are as follows.-
url: Set this to the RESTful API address.
-
Authentication Method: Select an authentication method supported by your data source API and configure the required authentication parameters.
-
Resource group connectivity: RestAPI data sources support only exclusive resource groups for Data Integration. Select an exclusive resource group for Data Integration and test the connectivity between the data source and the resource group.
-
-
Create a batch synchronization node and configure the synchronization task.
Create a batch synchronization node in DataWorks Data Studio. For more information, see Create a batch synchronization node. The key configurations are as follows.
-
Data Source configurations:
-
Data Source: Select the RestAPI data source created in the previous step.
-
Request Method: The sample API is a POST API. Select POST.
-
Return Data Structure: The response of the sample API is a JSON array. Select Array Data.
-
The JSON path for data storage: The data returned by the sample API is stored under the data field. Set this to data.
-
Header: The POST API in this example accepts a JSON request body. Set this to
{"Content-Type":"application/json"}. -
Request Param: Use request parameters together with scheduling parameters to synchronize data for the current day on a daily basis.
-
Set the request parameters to
{ "userId":16, "startTime":"${extract_day} 00:00:00", "endTime":"${extract_day} 23:59:59" } -
In the schedule settings, add one scheduling parameter:
extract_day=${yyyy-mm-dd}.
-
-
-
Data Destination configurations:
-
Data Source and Table: Select the MaxCompute partitioned table created in the previous step.
-
Partition Information: Use partition information together with scheduling parameters.
-
Set the partition information to
${bizdate}. -
In the schedule settings, add one scheduling parameter:
bizdate=$bizdate.
If the run date is 2023-01-05, the partition information value is 20230104.
-
-
-
Field Mapping configurations: Based on the data definition in the API, enter the column names from the RestAPI response. You can use dots (.) to separate nested columns. Column names are case-sensitive. After you add the columns, use The same name mapping or manually draw lines to create column mappings.
-
Test run
This practice uses scheduling parameters. After you complete the batch synchronization task configuration, click Run with Parameters in the toolbar at the top of the batch synchronization node page, enter the test scheduling parameter values as prompted, and run the synchronization task for testing. This button is displayed as a run icon with a parameter marker to the right of the Run button. After the test run is complete, you can view the runtime log at the bottom of the page to verify whether the scheduling parameter values meet your expectations.
Data verification
You can run an ad hoc query in Data Studio to verify whether the data is correctly synchronized to MaxCompute. The following is a sample query statement.
select * from ods_xiaobo_rest3 where ds='20230105' order by create_time;
In the statement, ods_xiaobo_rest3 is the MaxCompute partitioned table created in the previous step, and 20230105 is the partition value used during the test run.
After the query is complete, check the query results at the bottom of the page to verify whether the data is correctly synchronized to MaxCompute. The query results table should display the column values for user_id, name, axis, value, and create_time, as well as the partition ds value, which should be consistent with the JSON data returned by the API to confirm that nested columns are correctly mapped and synchronized.
Practice 4: Loop through a request parameter to read from a RestAPI
Sample scenario: API definition
In this practice, you iteratively read data from a RESTful API and write it to a MaxCompute partitioned table. The sample RESTful API is a self-built test GET API that returns temperature data based on the date, province, and city input parameters.
When you perform the actual operations, adjust the configurations based on the API you use. The API in this example is only used as a demonstration to walk you through the workflow.
-
Sample request:
http://TestAPIAddress:Port/rest/test5?date=2023-01-04&province=zhejiang&city=hangzhou -
Sample response:
{ "province": "P1", "city": "hz", "date": "2023-01-04", "minTemperature": "-14", "maxTemperature": "-7", "unit": "℃", "weather": "cool" } -
Sample API call in a testing tool: Send a GET request in an API testing tool such as Postman. Pass in the parameters date=2023-01-04, province=p1, and city=hz. The API returns a JSON response that contains the corresponding minTemperature (-14), maxTemperature (-7), unit (℃), and weather (COOL) information.
Preparation: Create a parameter table and a MaxCompute partitioned table
In this practice, data read from the API is synchronized to a MaxCompute partitioned table. You must first create a parameter table to store the province and city values for iteration, and then create a partitioned table to store the synchronized data.
When a partitioned table is used with the overwrite command, the partition overwrite effect is achieved. This makes the synchronization task rerunnable without data duplication, and partitioned tables are also easier to use for data analysis.
The CREATE TABLE statements are as follows:
Create the parameter table
CREATE TABLE IF NOT EXISTS `citys`
(
`province` STRING ,
`city` STRING
);
insert into citys
select 'shanghai','shanghai'
union all select 'zhejiang','hangzhou'
union all select 'sichuan','chengdu';
Create the MaxCompute partitioned table
CREATE TABLE IF NOT EXISTS ods_xiaobo_rest5
(
`minTemperature` STRING ,
`maxTemperature` STRING ,
`unit` STRING ,
`weather` STRING
)
PARTITIONED BY
(
`province` STRING ,
`city` STRING ,
`ds` STRING
)
LIFECYCLE 3650;
If you use DataWorks Standard Edition and submit the created tables to the production environment, you can view the tables in Data Map.
Configure the synchronization task
-
Add a RestAPI data source.
Add a RestAPI data source in your DataWorks workspace. For more information, see Add a RestAPI data source. In the Add RestAPI Data Source dialog, specify Data Source Name and Data Source Description, select Applicable Environment (development or production), and configure Default Request Header (default value:
{}). The key configurations are as follows:-
url: Set this to the RESTful API address.
-
Authentication Method: Select an authentication method supported by your data source API and configure the required authentication parameters.
-
Resource group connectivity: Select the specified resource group and test the connectivity.
-
-
Create an assignment node named setval_citys in DataStudio. For more information, see Create an assignment node.
The key configurations are as follows:
No.
Description
①
-
Assignment language: ODPS SQL
-
Assignment code:
SELECT province ,city FROM citys;
②
Rerun property: Set this to You can run again after successful or failed operation..
After the configuration is complete, submit and deploy the assignment node.
-
-
Create a for-each node in DataStudio. For more information, see Create a for-each node. The key configurations are as follows:
No.
Description
①
Rerun property: Set this to You can run again after successful or failed operation..
②
Upstream node dependency: Select the node from the previous step, which is the setval_citys node.
③
Node context parameter: Select the source of the input parameter.
④
Batch synchronization node: Configure the batch synchronization node inside the for-each node. For details, see the following steps.
-
Create a batch synchronization node and configure the synchronization task. For more information, see Create a batch synchronization node.
The key configurations are as follows:
No.
Description
①
Configure the scheduling parameters as follows:
bizdate=$[yyyymmdd-1] bizdate_year=$[yyyy-1] bizdate_month=$[mm-1] bizdate_day=$[dd-1]②
Configure the RestAPI request parameters. The province and city parameters come from the for-each node.
date=${bizdate_year}-${bizdate_month}-${bizdate_day}&province=${dag.foreach.current[0]}&city=${dag.foreach.current[1]}③
Configure the MaxCompute partition parameter. The province parameter comes from the for-each node.
province=${dag.foreach.current[0]}④
Configure the MaxCompute partition parameter. The city parameter comes from the for-each node.
city=${dag.foreach.current[1]}⑤
Configure the MaxCompute partition parameter. The ds parameter comes from the scheduling parameters.
ds=${bizdate}⑥
Based on the data definition in the API, enter the column names from the RestAPI response. Column names are case-sensitive. After you add the columns, use The same name mapping or manually draw lines to create column mappings.
After the configuration is complete, submit and deploy the for-each node.
Test run
-
After you successfully submit and deploy the assignment node and for-each node, go to the scheduled tasks in Operation Center and perform a backfill data operation on the assignment node. For more information, see Backfill data.
-
Select the business date for the backfill data task and the downstream nodes based on your actual scenario.
-
After the task runs, check the execution parameters in the backfill data instance details to verify whether they meet your expectations, and check the runtime log for correctness.
In this example, data is written to the
province=shanghai,city=shanghai,ds=20231215partition of the MaxCompute table.
Data verification
You can run an ad hoc query in Data Studio to verify whether the data is correctly synchronized to MaxCompute. The following is a sample query:
In the example, ods_xiaobo_rest5 is the MaxCompute partitioned table created in the Preparation step.
SELECT weather
,mintemperature
,maxtemperature
,unit
,province
,city
,ds
FROM ods_xiaobo_rest5
WHERE ds != 1
ORDER BY ds,province,city;
After the query is complete, check whether the data is correctly synchronized to MaxCompute.
|
weather |
mintemperature |
maxtemperature |
unit |
province |
city |
ds |
|
COOL |
3 |
9 |
°C |
shanghai |
shanghai |
20231215 |
|
HAZY |
-2 |
6 |
°C |
sichuan |
chengdu |
20231215 |
|
FOGGY |
19 |
28 |
°C |
zhejiang |
hangzhou |
20231215 |
|
SNOWY |
-16 |
-5 |
°C |
shanghai |
shanghai |
20231216 |
|
SNOWY |
-16 |
-8 |
°C |
sichuan |
chengdu |
20231216 |
|
SUNNY |
15 |
25 |
°C |
zhejiang |
hangzhou |
20231216 |
|
COOL |
-10 |
2 |
°C |
shanghai |
shanghai |
20231217 |
|
HAZY |
15 |
24 |
°C |
sichuan |
chengdu |
20231217 |
|
FOGGY |
4 |
11 |
°C |
zhejiang |
hangzhou |
20231217 |
|
HOT |
-14 |
-7 |
°C |
shanghai |
shanghai |
20231218 |
|
FOGGY |
-2 |
4 |
°C |
sichuan |
chengdu |
20231218 |
|
RAINY |
9 |
19 |
°C |
zhejiang |
hangzhou |
20231218 |