You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取ADF中ForEach循环的管道执行结果并实现留存或查询?

解决方案

方案一:直接用Kusto查询Azure Monitor日志(优先推荐)

前提配置

确保你的Synapse工作区已开启诊断设置,将管道运行日志发送到Log Analytics工作区(Kusto的数据源)。配置时需勾选PipelineRuns和ActivityRuns日志类别。

Kusto查询语句

以下查询可关联父管道、子管道、ForEach循环的执行日志,提取所需字段:

ActivityRuns
| where OperationName == "Execute Pipeline" // 匹配父管道调用子管道的活动
| extend ChildPipelineRunId = tostring(Properties["pipelineRunId"])
| join kind=inner (
    ActivityRuns
    | where ActivityType == "ForEach" // 匹配子管道中的ForEach活动
    | extend LoopItems = todynamic(Properties["items"])
    | mv-expand LoopItems
    | extend LoopValue = tostring(LoopItems)
    | project ChildPipelineRunId = PipelineRunId, LoopValue, ForEachStatus = Status, ForEachStartTime = StartTime
) on ChildPipelineRunId
| join kind=inner (
    PipelineRuns
    | project PipelineRunId, PipelineName, PipelineStartTime = StartTime
) on $left.ChildPipelineRunId == $right.PipelineRunId
| project 
    管道名称 = PipelineName,
    执行日期 = format_datetime(PipelineStartTime, 'yyyy-MM-dd HH:mm:ss'),
    循环值 = LoopValue,
    执行状态 = ForEachStatus
| sort by 执行日期 desc

补充说明

如果需要单独记录ForEach内DataFlow活动的状态,可再关联一层ActivityRuns筛选ActivityType == "DataFlow",通过ForEach的ActivityRunId关联获取DataFlow的执行状态。

方案二:管道内生成记录文件再导入Kusto

若无法直接使用Log Analytics,可通过管道自身生成记录文件后导入Kusto。

步骤1:子管道收集循环状态

  1. 在子管道中创建数组类型变量(如LoopStatusRecords),用于存储每个循环的信息。
  2. 在ForEach活动内部,执行完DataFlow后添加Set Variable活动:
    • 变量选择LoopStatusRecords
    • 值用表达式追加新记录(若循环项为复杂对象,用string(item())替代item()):
      @concat(variables('LoopStatusRecords'), json(concat('{"管道名称":"', pipeline().PipelineName, '","执行日期":"', utcnow(), '","循环值":"', item(), '","执行状态":"', activity('DataFlow活动名称').Status, '"}')))
      
  3. 子管道结束时,在Return Value中返回LoopStatusRecords变量。

步骤2:父管道写入文件

  1. 父管道调用子管道后,添加Copy Activity:
    • 源选择Inline,格式选JSON,内容填@activity('调用子管道活动名称').Output
    • 目标选择Blob存储(如ADLS Gen2),文件路径设为动态路径(如loop-records/@{utcnow('yyyyMMddHHmmss')}.json)

步骤3:导入Kusto

在Kusto中通过外部表或.ingest命令导入文件:

.ingest into table LoopStatusRecords (@'https://yourstorageaccount.dfs.core.windows.net/container/loop-records/*.json')
with (format = 'json')
最佳实践
  • 优先选择方案一:无需额外开发管道逻辑,实时性强,避免文件存储的维护成本。
  • 若使用文件记录,建议按天分区存储,便于Kusto批量导入和查询。
  • 处理复杂循环值时,确保序列化后的字符串格式正确,避免JSON解析错误。

内容的提问来源于stack exchange,提问作者Guilherme Matheus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 03:16:30