When you need to process a data set or run a task multiple times until a condition is met, use a do-while node to automate repetitive execution. This simplifies complex tasks and improves their execution efficiency and reliability. This topic uses an e-commerce order analysis example to demonstrate how to use a do-while node.
Background information
DataWorks provides the loop node (do-while node), which allows you to orchestrate a workflow within the do-while node. You can define the looping logic within the node, and then configure an end node to control the exit condition. You can also use a do-while node with an assignment node to iterate through a result set. This topic demonstrates how to use a do-while node with a simple order statistics example.
This topic uses the following example:
-
Source data
In the e-commerce industry, the daily volume of orders can be very large. For this reason, an order table is created in DataWorks that is partitioned by day based on the order date. This means that orders created on the same day are stored in the same partition, and the value of the partition field is the order date in
yyyyMMddformat. For example, the partition20220901stores all order data created onSeptember 1, 2022.| id | user_id | order_amount | ds | |----|---------|--------------|----------| | 1 | 1001 | 500 | 20220901 | | 2 | 1002 | 1500 | 20220901 | | 7 | 1003 | 890 | 20221021 | | 8 | 1004 | 240 | 20221021 | -
Data requirements
You need to calculate the order amount and order count for the
last 30 days,last 60 days, andlast 90 days, based on the first day of each month in 2022. For example,stat_day=20220901 stat_type=30drepresents the statistics for thelast 30 daysas of September 1, 2022.| stat_day | stat_type | order_total | order_amount_total | |----------|-----------|-------------|--------------------| | 20220901 | 30d | 10 | 0 | | 20220901 | 60d | 20 | 0 | | 20220901 | 90d | 30 | 0 | -
Requirement analysis
-
You need to generate 12 records, one for each month in 2022.
-
To calculate statistics for each data record, you need to query all partitions from the
last 1 month (30 days),last 2 months (60 days), andlast 3 months (90 days). The partition range for the query is dynamically calculated based on the first day of each month, rather than being a fixed range. -
In summary, you can use a do-while loop node to iterate 12 times to easily calculate monthly data, and use three SQL nodes to calculate data for
last 1 month (30 days),last 2 months (60 days), andlast 3 months (90 days), respectively. By using${dag.loopTimes}to represent the current month's iteration number, you can calculate the partitions for the first day of the current month and for the first day of the preceding one, two, and three months in the SQL nodes. This process ultimately fulfills the data statistics requirement.
-
Prerequisites
-
You have activated MaxCompute and created a MaxCompute project. For more information, see Activate MaxCompute and Create a MaxCompute project.
-
You have activated DataWorks Standard Edition or a later edition. For more information, see DataWorks purchase guide and Features of DataWorks editions.
-
Your DataWorks workspace is in standard mode. For more information, see Upgrade the mode of a workspace.
NoteWhen you create a DataWorks workspace, the Join the public beta for Data Development (Data Studio) option is disabled by default. This example is not applicable to public beta workspaces.
-
You have associated a MaxCompute engine with your workspace in DataWorks. For more information, see Associate a MaxCompute engine with a workspace.
Prepare data
-
Log on to the MaxCompute console. In the left-side navigation pane, choose to go to Data Development.
-
In the left-side navigation pane, click Data Development to go to DataStudio.
-
In the target workflow, under , right-click and select . Name the node, such as
init_test_data. -
In the
init_test_datanode, output the following SQL script.-- Create the orders table. CREATE TABLE orders ( id BIGINT ,user_id BIGINT -- User ID ,order_amount BIGINT -- Order amount ) PARTITIONED BY ( ds STRING -- Order date in yyyyMMdd format ) ; -- Insert order data. INSERT INTO orders PARTITION (ds = '20220901') VALUES (1,1001,500) ,(2,1002,1500); INSERT INTO orders PARTITION (ds = '20220905') VALUES (3,1005,260) ,(4,1002,780); INSERT INTO orders PARTITION (ds = '20221010') VALUES (5,1003,890) ,(6,1004,240); INSERT INTO orders PARTITION (ds = '20221021') VALUES (7,1003,890) ,(8,1004,240); INSERT INTO orders PARTITION (ds = '20221025') VALUES (9,1002,260) ,(10,1007,780); -- Create the order statistics table. CREATE TABLE orders_stat ( stat_day STRING -- The statistics date, which is the first day of each month. ,stat_type STRING -- The statistics type. ,order_total BIGINT -- The total number of orders. ,order_amount_total BIGINT -- The total order amount. ) ; -
Submit the
init_test_datanode to the production environment and backfill the data.-
On the node's toolbar, click the
Save and
Commit icons. When you commit the node, enter a change description and select whether to perform a code review and smoke testing as needed.Note-
You must set the rerun property and configure Parent Nodes in the scheduling configuration before you can commit the node.
-
If code review is enabled, the submitted node code must be approved by a reviewer before it can be deployed. For more information, see Code review.
-
To ensure that the scheduled node runs as expected, perform smoke testing on the task before you deploy it. For more information, see Smoke testing.
-
-
If you are using a workspace in standard mode, you must click Deploy in the upper-right corner to deploy the node after a successful commit. For more information, see Standard mode of a workspace and Deploy nodes.
-
On the right side of the node's toolbar, click O&M to go to the Operation Center. In the left-side navigation pane, choose . In the cycle task list, find and click
init_test_data. In the DAG view on the right, right-click theinit_test_datanode and select Test from the shortcut menu to run a test and initialize the data.
-
-
Freeze the node.
NoteThis step performs a one-time data initialization. After this node runs successfully once, you can Freeze it to prevent it from running periodically.
In the node task list, select this node, click the actions button at the bottom, and then click Freeze from the shortcut menu.
Procedure
Create a do-while node
Make sure that a MaxCompute engine is associated with the current workspace. Otherwise, you cannot proceed with the following operations. For more information, see Associate a MaxCompute engine with a workspace.
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 Data Development.
-
Create a do-while node.
-
On the Data Studio page, move the pointer over the
icon and choose .Alternatively, you can open a workflow, right-click General, and select .
-
In the Create Node dialog box, specify the node name and path.
-
Click OK.
-
Add Shell and ODPS SQL nodes
-
Double-click the do-while node to open its internal view.
-
Delete the default
sqlnode. Right-click thesqlnode inside the do-while node and click Delete Node. In the Delete dialog box, click OK. -
From the left-side component list, drag and into the canvas to create three ODPS SQL nodes and one Shell node.
-
Name the three ODPS SQL nodes
30_day,60_day, and90_day. -
Name the Shell node
echo.
-
-
Set the dependencies between the ODPS SQL and Shell nodes. Drag connection lines to set the
startnode as the parent of theechonode. Then, set theechonode as the parent of the30_day,60_day, and90_daynodes. Finally, set all three SQL nodes as parents of theendnode.
Configure node code
-
The
${dag.loopTimes}variable is a system-reserved built-in variable that represents the current loop iteration, starting from 1. Nodes inside a do-while node can directly reference this variable. For more information about built-in variables, see Built-in variables and Scheduling parameters. -
Unsaved changes will not take effect. After you modify the code in a Shell node, make sure to save it.
-
Configure the code for the
echonode. This node is used to output a log of the current loop count. Double-click theechonode to open the edit page of the Shell node and enter the following code.#!/bin/bash echo "loop times: ${dag.loopTimes}" -
Configure the code for the
30_daynode. This node is used to calculate order data for the last month on the first day of each month in 2022. Double-click the30_daynode and enter the following code on the edit page of the ODPS SQL node.INSERT INTO orders_stat SELECT CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01') AS stat_day ,'30d' AS stat_type ,COUNT(id) AS order_total ,nvl(SUM(order_amount),0) AS order_amount_total FROM orders WHERE -- The first day of the previous month ds >= REPLACE(ADD_MONTHS(TO_DATE(CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01'),'yyyyMMdd'),-1),'-','') -- The first day of the current month AND ds < CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01') ;The following DML statements and functions are used in the preceding code:
-
Configure the
60_daynode. This node is used to aggregate order data from the preceding two months on the first day of each month in 2022.INSERT INTO orders_stat SELECT CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01') AS stat_day ,'60d' AS stat_type ,COUNT(id) AS order_total ,nvl(SUM(order_amount),0) AS order_amount_total FROM orders WHERE -- The first day of two months ago ds >= REPLACE(ADD_MONTHS(TO_DATE(CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01'),'yyyyMMdd'),-2),'-','') -- The first day of the current month AND ds < CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01') ; -
Configure the
90_daynode. This node is used to calculate the order data from the last three months for the first day of each month in 2022.INSERT INTO orders_stat SELECT CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01') AS stat_day ,'90d' AS stat_type ,COUNT(id) AS order_total ,nvl(SUM(order_amount),0) AS order_amount_total FROM orders WHERE -- The first day of three months ago ds >= REPLACE(ADD_MONTHS(TO_DATE(CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01'),'yyyyMMdd'),-3),'-','') -- The first day of the current month AND ds < CONCAT('2022',LPAD(${dag.loopTimes},2,'0'),'01') ; -
Set the
endnode. This node controls when to exit the loop.
Commit node
Double-click the do-while node and perform the following operations in the node settings pane on the right:
-
You must configure the Rerun attribute and Parent Nodes before you can submit the node.
-
Code review helps ensure the quality of task code and prevents errors caused by deploying unreviewed code directly to the production environment. If code review is enabled, the submitted node code must be approved by reviewers before it can be deployed. For more information, see Code review.
-
Click the
icon in the toolbar to save the node. -
Click the
icon in the toolbar to submit the node.When you submit the node, enter a Change Description in the Submission dialog box and specify whether to initiate a code review after the node is submitted.
Important-
You must configure the Rerun attribute and Parent Nodes before you can submit the node.
-
Code review helps ensure the quality of task code and prevents errors caused by deploying unreviewed code directly to the production environment. If code review is enabled, the submitted node code must be approved by reviewers before it can be deployed. For more information, see Code review.
If you use a workspace in standard mode, you must click Deploy in the upper-right corner of the node editing page to deploy the task to the production environment after it is submitted. For more information, see Deploy nodes.
-
Test node
The commit and deploy process for a do-while node is the same as for a regular node. However, direct testing from DataStudio is not supported.
When DataWorks is in standard mode, you cannot directly test a do-while node from the Data Studio interface.
To test and verify the running results of a do-while node, you must submit and deploy the task that contains the do-while node to Operation Center, and then run the do-while node task from the Operation Center page. If you use values passed by an assignment node inside the do-while node, run both the assignment node and the do-while node together during testing in Operation Center.
-
Go to the cycle task page to backfill data.
-
In the upper-right corner of the page, click Operation Center to go to the Operation Center.
-
In the left-side navigation pane, choose .
-
Find the required node. In the DAG view on the right, right-click the
do_while_testnode and choose .Because the SQL script logic in this example does not depend on the Data Timestamp, you can run the node with the default Data Timestamp.
-
Go to the backfill instance page, click the task name
do_while_test, right-click the task name in the DAG view on the right, and then click View Runtime Log in the shortcut menu.
-
-
View the result table data.
Create a new ODPS SQL node and execute the following SQL statement:
SELECT * FROM orders_stat;Output:
| stat_day | stat_type | order_total | order_amount_total | |----------|-----------|-------------|--------------------| | 20220101 | 60d | 0 | 0 | | 20220101 | 30d | 0 | 0 | | 20220101 | 90d | 0 | 0 | | 20220201 | 60d | 0 | 0 | | 20220201 | 30d | 0 | 0 | | 20220201 | 90d | 0 | 0 | | 20220301 | 30d | 0 | 0 | | 20220301 | 60d | 0 | 0 | | 20220301 | 90d | 0 | 0 | | 20220401 | 30d | 0 | 0 | | 20220401 | 90d | 0 | 0 | | 20220401 | 60d | 0 | 0 | | 20220501 | 30d | 0 | 0 | | 20220501 | 90d | 0 | 0 | | 20220501 | 60d | 0 | 0 | | 20220601 | 60d | 0 | 0 | | 20220601 | 30d | 0 | 0 | | 20220601 | 90d | 0 | 0 | | 20220701 | 90d | 0 | 0 | | 20220701 | 30d | 0 | 0 | | 20220701 | 60d | 0 | 0 | | 20220801 | 60d | 0 | 0 | | 20220801 | 90d | 0 | 0 | | 20220801 | 30d | 0 | 0 | | 20220901 | 90d | 0 | 0 | | 20220901 | 60d | 0 | 0 | | 20220901 | 30d | 0 | 0 | | 20221001 | 90d | 4 | 3040 | | 20221001 | 60d | 4 | 3040 | | 20221001 | 30d | 4 | 3040 | | 20221101 | 30d | 16 | 8860 | | 20221101 | 90d | 20 | 11900 | | 20221101 | 60d | 20 | 11900 | | 20221201 | 90d | 20 | 11900 | | 20221201 | 60d | 16 | 8860 | | 20221201 | 30d | 0 | 0 |