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

如何在Apache Beam Python SDK中同时使用with_outputs与with_output_types?

Apache Beam 中 with_outputs 与 with_output_types 的组合使用

with_outputs 和 with_output_types 完全可以组合使用,核心要注意类型参数的顺序必须和输出定义的顺序严格对应:

  • with_output_types 的第一个参数对应 with_outputs 中指定的 main 输出类型;
  • 后续参数依次对应 with_outputs 里列出的额外输出(按书写顺序)。

以你给出的代码为例,正确的组合用法如下:

(words | beam.ParDo(ProcessWords(), cutoff_length=2, marker='x')
             .with_outputs('above_cutoff_lengths', 'marked strings',
                           main='below_cutoff_strings')
             .with_output_types(str, str, str)
)

这里三个类型参数分别对应:

  1. 主输出 below_cutoff_strings 的类型 str
  2. 额外输出 above_cutoff_lengths 的类型 str
  3. 额外输出 marked strings 的类型 str

这种组合方式能明确声明多输出PTransform各分支的数据类型,帮助静态类型检查工具或IDE提供更准确的代码提示,避免类型相关的隐性错误。

内容的提问来源于stack exchange,提问作者Mike Williamson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 09:57:08