Set Variable Activity(YAML 类型标识为 set_variable)在 Pipeline 执行过程中动态修改变量的值。支持两种用途:
适用场景:
将变量设置为一个固定值或引用值:
- name: set_status type: set_variable variableType: PIPELINE_INNER variableName: process_status value: rawValue: "completed" position: x: "200" y: "100"
# 引用其他变量或参数的值 - name: copy_date type: set_variable variableType: PIPELINE_INNER variableName: current_date value: rawValue: "{{pipeline.parameters.biz_date}}" position: x: "200" y: "100"
通过计算表达式设置变量值:
- name: increment_counter type: set_variable variableType: PIPELINE_INNER variableName: attempt_count value: calculateValue: left: "{{variables.attempt_count}}" op: PLUS right: "1" position: x: "200" y: "100"
字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| String | 是 | 固定为 |
| Enum | 是 | 固定为 |
| String | 是 | 目标变量名(需在 Pipeline variables 中预先定义) |
| String | 二选一 | 直接赋值:固定值或引用表达式 |
| Object | 二选一 | 计算赋值 |
| String | 是 | 左操作数 |
| Enum | 是 | 运算符 |
| String | 是 | 右操作数 |
op 值 | 含义 | 示例 |
|---|---|---|
| 加法 |
|
| 减法 |
|
| 字符串连接 |
|
注意
左值和右值均为字符串类型。使用 PLUS 或 MINUS 运算时,两侧值需可解析为数字。
Pipeline 返回值用于向外部输出执行结果,父 Pipeline 的 Execute Pipeline 节点可获取这些值。
- name: set_output type: set_variable variableType: PIPELINE_OUTPUT outputVariables: - name: total_records description: "本次处理的总记录数" default: "{{variables.record_count}}" - name: status description: "执行状态" default: "success" - name: output_table description: "输出表名" default: "dw.order_summary" position: x: "600" y: "100"
字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
| String | 是 | 固定为 |
| Enum | 是 | 固定为 |
| Array | 是 | 返回值列表 |
| String | 是 | 返回值名称 |
| String | 否 | 返回值描述 |
| String | 是 | 返回值内容(支持变量引用) |
子 Pipeline 通过 PIPELINE_OUTPUT 设置的返回值,可在父 Pipeline 的后续节点中引用:
# 父 Pipeline 中引用子 Pipeline 的返回值 parameterValues: upstream_count: "{{activities.run_sub_pipeline.output.total_records}}" upstream_status: "{{activities.run_sub_pipeline.output.status}}"
注意
父 Pipeline 的 Execute Pipeline 节点需设置 waitingSuccess: true 才能获取返回值。
variables: - name: loop_count type: Integer defaultValue: "0" activities: - name: loop_task type: do_while condition: left: "{{variables.loop_count}}" op: LESS_THAN right: "10" activities: - name: do_work type: sql source: WORKSPACE path: /Workspace/Users/zhang3/sql/batch.sql engineType: emr_serverless_spark engineQueue: default position: x: "100" y: "50" - name: inc_counter type: set_variable variableType: PIPELINE_INNER variableName: loop_count value: calculateValue: left: "{{variables.loop_count}}" op: PLUS right: "1" dependsOn: activities: - name: do_work position: x: "300" y: "50"
variables: - name: data_ready type: String defaultValue: "false" activities: - name: check_data type: notebook # ... Notebook 中根据检查结果调用 API 设置变量 position: x: "200" y: "100" - name: mark_ready type: set_variable variableType: PIPELINE_INNER variableName: data_ready value: rawValue: "true" dependsOn: activities: - check_data position: x: "400" y: "100" - name: branch type: if_condition condition: left: "{{variables.data_ready}}" op: EQUAL_TO right: "true" dependsOn: activities: - mark_ready position: x: "600" y: "100"
- name: build_path type: set_variable variableType: PIPELINE_INNER variableName: output_path value: calculateValue: left: "/data/output/" op: CONCAT right: "{{pipeline.parameters.biz_date}}" position: x: "200" y: "100" # 结果:output_path = "/data/output/2026-06-01"
建议 | 说明 |
|---|---|
预先定义变量 | PIPELINE_INNER 引用的变量必须在 Pipeline 的 |
注意执行顺序 | Set Variable 必须在引用该变量的 Activity 之前执行(通过 dependsOn 保证)。 |
循环中更新条件 | 在 Do While 循环体中务必通过 Set Variable 更新条件变量。 |
返回值命名清晰 | PIPELINE_OUTPUT 的返回值名称应语义明确,便于父 Pipeline 引用。 |
避免过度使用 | 简单的参数传递优先使用 parameterValues,仅在需要动态修改时使用 Set Variable。 |