我有一些工作需要重复完成。例如,假设我想掷 2000 个骰子并收集结果。需要注意的是,骰子的 throw 取决于 PCollection
如何使用 Dataflow 来完成此操作?
我尝试使用 PCollectionList
,但结果是我的数据流太大而无法启动 (> 10 MB)。以下是我想要执行的操作的示例(使用 PCollectionList
):
// I'd like to operate on things 2000 times.
PCollection<Thing> things = ...;
List<PCollection<ModifiedThing>> modifiedThingsList = new ArrayList<>();
for (int i = 0; i < 2000; ++i) {
modifiedThingsList.add(things.apply(ParDo.of(thing -> modify(thing)));
}
PCollection<ModifiedThing> modifiedThings = PCollectionList.of(modifiedThingsList).apply(Flatten.pCollections());
由于上图的 JSON 表示对于 Dataflow 来说太大,因此我需要一种不同的方式来表示此逻辑。有任何想法吗?
最佳答案
ParDo
或 FlatMapElements
可以为每个输入返回任意数量的输出。例如:
PCollection<ModifiedThing> modifiedThings = things.apply(
ParDo.of(new DoFn<Thing, ModifiedThing>() {
public void processElement(ProcessContext c) {
for (int i = 0; i < 2000; ++i) {
c.output(modify(c.element()));
}
}
}));
警告:如果您要立即将其他 ParDo
应用于 modifiedThings
,be careful with fusion ,自 2000 年以来是一个相当高的扇出因子。防止融合的一个很好的示例代码片段是 here .
关于java - 如何使用 Dataflow 多次执行相同的工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41390599/