如何在 Spring 响应式编程中处理两个 Flux?

How to deal with two Fluxes in Spring Reactive Programing?

我有两个助焊剂

Flux<Foo> foo;
Flux<Bar> bar;

class Foo 
String id
String prop1
String barId


class Bar 
String barId
String color
boolean isInFoo

foo 和 bar 都有 barId 属性。 foo 的大小始终等于或小于 bar 的大小,但通常它远小于 bar 的大小(Flux 中的 Bars 数量。例如,Foo 可以代表一篮子 Bar 项目中的一些选定项目,尽管两者是不同的对象。

Bar 有一个布尔标志 isInFoo,默认值为 false,如果 Foo 有那个 barId,则设置为 true。

如何遍历 bar 并查找每个 bar 在 foo 中是否有 barId,如果有,将 isInFoo 设置为 true。

我也有空

Mono<Foo> findByBarId(String barId) {}

除了以上Flux<Foo> findAll() {}

请注意,findByBarId 和 findAll 是昂贵的数据库操作。

假设您有以下两个通量:

Flux<Foo> foos;
Flux<Bar> bars;

您可以按如下方式修改 Bar 对象的 Flux:

bars.flatMap(bar -> findByBarId(bar.getBarId())
                    .flatMap(foo -> {
                         bar.setIsInFoo(true);
                         return Mono.just(bar);
                    }).switchIfEmpty(Mono.just(bar)));