如何将具有单个元素的 PCollection<Long> 转换为内存中的 java 集合?

How to convert a PCollection<Long> having a single element to a java in-memory collection?

所以我想做的是将 Count 转换的输出存储在现在只有一个元素的 PCollection<Long> 中。如果有任何方法可以将其转换为 Java 内存中集合,那将非常方便。本质上我正在寻找的是 Create.of() 转换的否定。任何的想法?

看看 Scio 中的具体化。

在 Beam 中,没有提供执行此操作的方法。也就是说,无论您想进行何种后续计算,都可以将单个元素作为 ParDo 的侧输入。我建议您在执行 Count 时直接转换为 PCollectionView;对于具有标识元素的组合器来说,这是一个很好的做法。

PCollectionView<Long> count =
    input.apply(Combine.globally(Count.combineFn()).asSingletonView())

dummyCollection.apply(ParDo.of(new DoFnConsumingTheCount(count)).withSideInputs(count));