<p>一般来说,不能将<code>PCollection</code>转换为列表。在</p>
<p><code>PCollection</code>是一个可能是无界且无序的项的集合。Beam允许您对<code>PCollection</code>应用转换。将<code>PTransform</code>应用于<code>PCollection</code>会产生另一个<code>PCollection</code>。应用转换的过程可能分布在一组机器上。所以在一般情况下,不可能把这样的东西转换成本地内存中的元素集合。在</p>
<p>组合器只是<code>PTransforms</code>的一个特殊类。他们所做的是将他们看到的所有元素累加起来,对这些元素应用一些组合逻辑,然后输出组合的结果。例如,组合器可以查看传入的元素,对它们进行求和,然后作为结果输出总和。这种组合器将元素的<code>PCollection</code>转换为这些元素和的<code>PCollection</code>。在</p>
<p><code>beam.combiners.ToList</code>只是应用于<code>PCollection</code>的另一个转换,它可能在一组工作机器上产生另一个<code>PCollection</code>。但是在生成输出元素之前,它并不真正进行任何复杂的组合,它只将所有的seen元素累加到一个列表中,然后输出seen元素的列表。因此,它获取键-值对(在多台计算机上)的元素,将它们放入列表中,然后输出这些列表。在</p>
<p>缺少的是从潜在的多台计算机中获取这些列表并在需要时将它们加载到本地程序中的逻辑。这个问题很难用一般的方法(在所有的运行程序、所有可能的IOs和管道结构之间)解决。在</p>
<p>解决方法之一是向管道中添加另一个步骤,将组合的输出(例如总和或列表)写入公共存储(例如,某个数据库中的表或文件)中。然后,当管道完成时,您的程序就可以从该位置加载管道执行的结果。在</p>
<p>有关详细信息,请参阅文档:</p>
<ul>
<li>梁执行模型:<a href="https://beam.apache.org/documentation/execution-model/" rel="nofollow noreferrer">https://beam.apache.org/documentation/execution-model/</a></li>
<li>设计管道:<a href="https://beam.apache.org/documentation/pipelines/design-your-pipeline/" rel="nofollow noreferrer">https://beam.apache.org/documentation/pipelines/design-your-pipeline/</a></li>
<li>光束编程指南:<a href="https://beam.apache.org/documentation/programming-guide/" rel="nofollow noreferrer">https://beam.apache.org/documentation/programming-guide/</a></li>
</ul>