如何使用 Dataflow 多次执行相同的工作?
How to do the same work multiple times using Dataflow?
我有一些工作需要重复完成。例如,假设我想掷 2000 个骰子并收集结果。需要注意的是,掷骰子取决于 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 表示对于数据流来说太大了,我需要一种不同的方式来表示这个逻辑。有任何想法吗?
ParDo
或 FlatMapElements
可以 return 每个输入的任意数量的输出。例如:
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, since 2000 is a pretty high fan-out factor. A good example code snippet for preventing fusion is here。
我有一些工作需要重复完成。例如,假设我想掷 2000 个骰子并收集结果。需要注意的是,掷骰子取决于 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 表示对于数据流来说太大了,我需要一种不同的方式来表示这个逻辑。有任何想法吗?
ParDo
或 FlatMapElements
可以 return 每个输入的任意数量的输出。例如:
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, since 2000 is a pretty high fan-out factor. A good example code snippet for preventing fusion is here。