All Products
Search
Document Center

DataWorks:Configure a for-each node

Last Updated:Mar 26, 2026

A for-each node iterates over a result set passed by an upstream assignment node and runs an inner workflow for each entry. This topic walks through a complete example: an assignment node outputs two entries, and the for-each node prints the current loop count for each iteration.

Key concepts

TermDescription
for-each nodeA node that iterates over an input dataset and runs an inner workflow for each entry
assignment nodeThe upstream node that produces the result set the for-each node iterates over; its built-in output parameter is outputs
inner nodeA node inside the for-each node's internal workflow; by default, a start node, a Shell node, and an end node
loopDataArrayThe for-each node's built-in input parameter that receives the dataset to iterate over
loop countThe number of the current iteration, exposed via ${dag.loopTimes}

Prerequisites

Before you begin, ensure that you have:

Limitations

LimitationWorkaround
For-each nodes cannot be tested directly in DataStudioUse data backfill in Operation Center: select both the assignment node and the for-each node, then run them together
Running only the for-each node does not produce assignment node outputAlways use data backfill and select both nodes to get the full output
The start and end nodes inside a for-each node have fixed logicReplace only the inner Shell node; the start and end nodes cannot be edited
Unsaved changes to inner node code are not included at commit timeSave the inner Shell node manually before committing the for-each node

Built-in variables

The following variables are available inside a for-each node's inner workflow.

VariableDescription
${dag.foreach.current}The current data entry being processed
${dag.loopDataArray}The full input dataset
${dag.offset}The zero-based index of the current iteration (offset from 1)
${dag.loopTimes}The current loop count; equals ${dag.offset} + 1

For more details and examples, see Built-in variables and Examples of variable values.

Set up the workflow

Create a workflow with an assignment node as the ancestor and a for-each node as the descendant.

遍历节点
  1. Go to the DataStudio page. Log on to the DataWorks console. In the top navigation bar, select the desired region. In the left-side navigation pane, choose Data Development and Governance > Data Development. On the page that appears, select the desired workspace and click Go to Data Development.

  2. Create an auto triggered workflow.

  3. Create a for-each node.

    1. In the Scheduled Workflow pane, move the pointer over the 新建 icon and choose Create Node > General > for-each. Alternatively, right-click the workflow in the Business Flow section and choose Create Node > General > for-each.

    2. In the Create Node dialog box, set the Name and Path parameters.

    3. Click Confirm.

  4. Create an assignment node.

    1. Double-click the workflow to open its configuration tab. Click + Create Node and drag Assignment Node from the General section onto the canvas. For more information about assignment nodes, see Configure an assignment node.赋值

    2. In the Create Node dialog box, set the Name and Path parameters. The assignment node is placed in the current workflow by default.

    3. Click Confirm.

  5. Drag a directed line from the assignment node to the for-each node to configure the assignment node as the ancestor.

    依赖

Configure the assignment node

  1. On the workflow configuration tab, double-click the assignment node. The node's configuration tab opens.

  2. Select SHELL from the Language drop-down list.

  3. Enter the following code in the code editor:

    echo 'this is name,ok';
  4. Click the Properties tab in the right-side navigation pane. In the Input and Output Parameters section, check the Output Parameters table. The outputs parameter is the assignment node's built-in output parameter.

    outputs

  5. Click the 保存 icon to save the node.

  6. Click the 提交 icon to commit the node. In the Submit dialog box, set the Change description parameter. Determine whether to enable code review based on your requirements. If your workspace is in standard mode, click Deploy in the upper-right corner after committing to deploy the node to the production environment. See Deploy nodes.

    Important

    Configure the Rerun and Parent Nodes parameters on the Properties tab before committing. If code review is enabled, the node can be deployed only after the code passes review. See Code review.

Configure the for-each node

  1. Double-click the for-each node. The configuration tab opens, showing the default start, Shell, and end nodes. Replace the Shell node with another node type if needed: This example uses the default Shell node.

    • To use a Shell node, configure it directly.

    • To use a different node type, delete the default Shell node and create the required type.

  2. Configure the Shell node.

    1. Double-click the Shell node. The node's configuration tab opens.

    2. Enter the following code in the code editor:

      echo ${dag.loopTimes} ----Display the current number of loops.
      Note

      Save the Shell node manually after editing. No save reminder appears when you commit the for-each node. Unsaved changes are not included in the committed version.

  3. Bind the assignment node's output to the for-each node's input.

    1. On the for-each node's configuration tab, click the Properties tab in the right-side navigation pane.

    2. In the Input and Output Parameters section, find loopDataArray in the Input Parameters table and click Change in the Actions column.

    3. In the Value Source column, select the outputs parameter of the assignment node from the drop-down list, then click Save.

      outputs

      Note

      Skipping this step causes an error when you commit the for-each node.

  4. Click the 保存 icon to save the for-each node.

  5. Click the 提交 icon to commit the for-each node. In the Commit dialog box, select the inner nodes to commit, enter a description, and click Commit. If your workspace is in standard mode, click Deploy after committing. See Deploy nodes.

    Important

    Configure the Rerun and Parent Nodes parameters on the Properties tab before committing.

Test the for-each node and view results

For-each nodes cannot be tested in DataStudio. Use data backfill in Operation Center to run both the assignment node and the for-each node together.

  1. On the node configuration tab, click Operation Center in the upper-right corner.

  2. In the left-side navigation pane, choose Cycle Task Maintenance > Cycle Task.

  3. Find the for-each node and click DAG in the Actions column to open its directed acyclic graph (DAG). Right-click the assignment node and choose Run > Current and Descendent Nodes Retroactively. Configure the parameters in the Patch Data dialog box and click OK.

    补数据

  4. Refresh the Patch Data page. After the data backfill instance finishes, click DAG in the Actions column of the instance.

  5. Right-click the assignment node in the DAG and select View Runtime Log to verify its output.

    赋值节点结果

  6. On the Patch Data page, right-click the for-each node in the DAG and select View Internal Nodes.

    内部节点

  7. In the middle pane, click Loop 1. Right-click the Shell node in the DAG and select View Runtime Log.

    运行日志

    The log shows the output of the Shell node for the first loop.1

  8. Repeat for Loop 2 to verify the second iteration.

    2

What's next