如何在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) )
这里三个类型参数分别对应:
- 主输出
below_cutoff_strings的类型str - 额外输出
above_cutoff_lengths的类型str - 额外输出
marked strings的类型str
这种组合方式能明确声明多输出PTransform各分支的数据类型,帮助静态类型检查工具或IDE提供更准确的代码提示,避免类型相关的隐性错误。
内容的提问来源于stack exchange,提问作者Mike Williamson
相关产品推荐
相关产品推荐

