DataWorks provides the do-while node, which implements "execute first, then check" loop logic. This node is ideal when you need to repeat a series of tasks until a dynamic condition is met, such as polling to check for a file's existence or processing data in batches until the source is empty. You can orchestrate a task flow within the loop body and use a dedicated End node to control the loop's exit.
Use cases
In data development, the do-while node simplifies the design of workflows that need to repeatedly execute tasks based on a condition. Common use cases include:
API polling: Repeatedly call an API until it returns a specific status, such as
SUCCESS, or until the response data meets your requirements.Data readiness checks: Wait for an upstream file or data partition to be generated. The loop checks for the data in each iteration and exits once it is available, triggering downstream tasks.
Batch data processing: Process a list of items, such as table names or date partitions, provided by an upstream node like an assignment node. The do-while node iterates through the list, processing one item at a time until the list is exhausted.
Status synchronization: Repeatedly check the status of an external service. Once the service status changes to a desired state, such as "Available" or "Complete", the loop exits and triggers the subsequent data synchronization or processing workflow.
Requirements
-
Version requirement: This feature is available only in DataWorks Standard Edition and later versions.
-
Permission requirement: Your RAM user must be a member of the target workspace with the Development or Workspace Manager role. For more information, see Add members to a workspace.
How it works
The do-while node acts as a loop container based on an "execute first, then check" mechanism.
Start node → loop body (execute tasks) →
End node (evaluate condition)
Start and execute: The loop starts at the
Start node, and all tasks in the loop body are executed at least once.Condition evaluation: After the loop body completes, the
End node runs. You write your conditional logic in this node.Loop decision:
If the
End node outputs the string True, a new iteration begins.If the
End node outputs the string False, the loop terminates, and the entire do-while node run is considered successful.
Key feature: The loop body always executes at least once, regardless of the initial condition.
Node components
Double-click a do-while node to open its internal canvas, which consists of three main parts:
Start node: The entry point of the loop. It cannot be edited or deleted.Loop body: A customizable canvas for your tasks. You can click Create Internal Node to add various types of internal nodes, such as Shell, SQL, and Python, to form the repeatable business process.
End node: The decision-making node of the loop. It is an assignment node that uses ODPS SQL, Shell, or Python to output a TrueorFalsestring. This string determines whether the loop continues.
Built-in variables
In the loop body and End node of a do-while node, you can use the following built-in variables to access the current loop state and input data:
Variable | Description |
System built-in variables | |
| The current iteration number, starting from 1. |
| The offset of the current iteration, starting from 0. It is equivalent to |
Used with an assignment node | |
| Returns the entire result set from the upstream node, which is typically a two-dimensional array. |
| Returns the number of rows (elements) in the result set. |
| Retrieves the row of data that the current loop is processing and returns it as a comma-separated string, such as |
| Returns the value at row |
Example:${dag.input[0][1]}extracts the value"beijing"from the second column of the first row.
Limitations
Edition requirement: This feature is available only in DataWorks Standard Edition and later versions.
Iteration limit: The default maximum number of iterations is 128, and can be adjusted to a maximum of 1024. Exceeding this limit causes the task to fail.
Execution model: The node uses a serial execution model, which means each iteration must be completed before the next one begins.
Debugging limitation: You cannot run and test a do-while node directly in DataStudio. You must first publish the workflow and then use the Backfill Data feature in Operation Center to verify its behavior.
Data dependencies: If the loop depends on an upstream assignment node, be sure to backfill data starting from the upstream node to ensure the data lineage is complete.
Flow control: If you use a branch node within the loop body, ensure all branches eventually converge at a merge node to prevent a broken workflow.
Procedure: Create a simple loop task
This example shows how to create a task that loops five times. In each iteration, the task prints the current loop number.
Step 1: Configure the loop body
Create a
do-whilenode in your workflow.Double-click the node to open its internal canvas. In the loop body area, click Create Internal Node, select Shell, and name the node
print_loop_times.Right-click the Shell node and select Open Node.
In the code editor, enter the following command:
# Use the built-in variable ${dag.loopTimes} to get the current loop number echo "This is loop number: ${dag.loopTimes}"Click the save icon to save your changes.
Step 2: Define the exit condition
Return to the do-while internal canvas. Right-click the
End node and select Open Node.Switch the language to Python.
Enter the following Python code:
# If the loop count is less than 5, continue the loop; otherwise, exit. if ${dag.loopTimes} < 5: print True else: print FalseSave the End node.
Step 3: Publish, run, and verify
Return to the main workflow canvas and click the Publish button in the toolbar to publish the entire workflow.
Go to Operation Center and locate the
do-whilenode.Right-click the node and select Backfill Data > Current Node to start a test run.
After the instance runs successfully, right-click it again and select View Internal Nodes.
Review the five iteration records. Expand one of the iterations, for example, the fifth one. Right-click the internal
print_loop_timesinstance and select View Runtime Log. You should see the following output:This is loop number: 5
Advanced use case: Process a data list
A common and practical pattern is to use a do-while node to iterate over and process a dataset produced by an upstream assignment node. In this scenario, an upstream ODPS SQL assignment node queries two rows of user information, and the do-while node processes each row.
Step 1: Configure the assignment node
Create an assignment node (for example, named
assign_sql_data) and set it as an upstream dependency of thedo-whilenode.In the node, use ODPS SQL to query data:
SELECT 'user_A', 'beijing' UNION ALL SELECT 'user_B', 'shanghai';Save the node. The
outputsparameter will contain a two-dimensional array with two rows of data.
Step 2: Configure the do-while node
In the Scheduling Configurations panel on the right side of the
do-whilenode, find the Input Parameters section.Click Add and configure the following parameter:
Name:
input(customizable)Value Source: Select
assign_sql_data.outputs
Create a new Shell node in the loop body of the
do-whilenode and enter the following script to process the current row:# Get the data row being processed in the current iteration echo "Processing data row: ${dag.input[${dag.offset}]}"Open the
End node and use Python to set the exit condition:# Continue looping if there are more data rows to process. if ${dag.loopTimes} < ${dag.input.length}: print True else: print FalsePublish the workflow and, in Operation Center, backfill data starting from the
assign_sql_datanode to ensure that data flows correctly into the loop.
After the run is successful, you can check the runtime logs to see that the two iterations processed "user_A,beijing" and "user_B,shanghai" respectively.
Appendix: Do-while vs. for-each nodes
Feature | Do-while node | For-each node |
Core logic | Condition-driven: Repeats execution until a condition is no longer met. | Data-driven: Executes once for each element in an input list. |
Number of iterations | Indeterminate, depends on when the condition is met. | Determinate, equal to the number of elements in the input list. |
Execution guarantee | Executes at least once, even if the initial condition is not met. | If the input list is empty, it does not execute at all. |
Use cases | Polling, waiting, status checks, and batch processing until a source is empty. | Batch processing a known list, such as synchronizing a set of tables or processing multiple partitions. |
Control method | The End node outputs | Automatically ends after iterating through all elements. |