gpt4 book ai didi

java - 您可以通过 Flux.zip 压缩一个单声道和一个通量,并为每个通量值重复单声道值吗?

转载 作者:行者123 更新时间:2023-12-05 08:31:13 25 4
gpt4 key购买 nike

是否可以像下面的代码那样做?我有一个进行 API 调用的服务和另一个返回值流的服务。我需要通过 API 调用返回的值修改每个值。

return Flux.zip(
someMono.get(),
someFlux.Get(),
(d, t) -> {
//HERE D IS ALWAYS THE SAME AND T IS EVERY NEW FLUX VALUE
});

我已经为 Mono 尝试使用 .repeat() 并且它有效,但是每次有一个新的 Flux 值时它都会调用该方法并且它是一个 API 调用,所以它不是很好。

这可能吗?

最佳答案

这将说明如何将通量与单声道组合,这样每次通量发射时,单声道也会发射。

假设您有这样的助焊剂和单声道:

 // a flux that contains 6 elements.
final Flux<Integer> userIds = Flux.fromIterable(List.of(1,2,3,4,5,6));

// a mono of 1 element.
final Mono<String> groupLabel = Mono.just("someGroupLabel");

首先,我将向您展示尝试压缩 2 的错误方法,我试过,我想其他人也会尝试:

 // wrong way - this will only emit 1 event 
final Flux<Tuple2<Integer, String>> wrongWayOfZippingFluxToMono = userIds
.zipWith(groupLabel);

// you'll see that onNext() is only called once,
// emitting 1 item from the mono and first item from the flux.
wrongWayOfZippingFluxToMono
.log()
.subscribe();

wrong way of zipping up the flux and mono

 // this is how to zip up the flux and mono how you'd want, 
// such that every time the flux emits, the mono emits.
final Flux<Tuple2<Integer, String>> correctWayOfZippingFluxToMono = userIds
.flatMap(userId -> Mono.just(userId)
.zipWith(groupLabel));

// you'll see that onNext() is called 6 times here, as desired.
correctWayOfZippingFluxToMono
.log()
.subscribe();

correct way of zipping up the flux and mono

关于java - 您可以通过 Flux.zip 压缩一个单声道和一个通量,并为每个通量值重复单声道值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com