AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77352712
Accepted
D-Dᴙum
D-Dᴙum
Asked: 2023-10-24 21:52:06 +0800 CST2023-10-24 21:52:06 +0800 CST 2023-10-24 21:52:06 +0800 CST

反应式变换函数的行为不符合预期

  • 772

当我提取各个步骤并单独执行它们时,我创建的反应器变换函数不会给出相同的结果。

我有包含价格类型列表的 DTO 记录。

public record CarrierDto(MetaData metaData, List<Daily> dailySeries) {

    public record MetaData(String name, String information) {};
    public record Daily(String date, Integer price) {};
}

Mono<CarrierDto>我想从a中提取Flux<Daily>然后将其转换为Flux<Price>.

@Table("price")
public record Price (
        @Column("p_date") String date,
        @Column("p_price") Integer price) {
}

为了进行这种转换,我有这个功能:

static Function<Mono<CarrierDto>, Flux<Price>> transform = carrierDtoMono ->
            carrierDtoMono.map(CarrierDto::dailySeries)
                    .flatMapMany(Flux::fromIterable)
                    .map(daily -> new Price(daily.date(), daily.price()));

然而,当我尝试使用此功能时,订阅时仅给出一个价格。如果我提取每个单独的步骤并执行提供的所有价格。例如这个测试类:

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

public class TestMain {

    static Function<Mono<CarrierDto>, Flux<Price>> transform = carrierDtoMono ->
            carrierDtoMono.map(CarrierDto::dailySeries)
                    .flatMapMany(Flux::fromIterable)
                    .map(daily -> new Price(daily.date(), daily.price()));

    public static void main(String[] args) {
        final CarrierDto dto = new CarrierDto(new CarrierDto.MetaData("Test", "Test information"),
                Arrays.asList(new CarrierDto.Daily("2023-01-01", 100),
                        new CarrierDto.Daily("2023-01-02", 101),
                        new CarrierDto.Daily("2023-01-03", 102),
                        new CarrierDto.Daily("2023-01-04", 103)));

        System.out.println("Using Transform function.");
        Mono.just(dto)
                .transform(transform)
                .subscribe(price -> System.out.println(String.format("Date: %s Price: %d", price.date(), price.price())));

        System.out.println("Without transform function.");

        final Mono<List<CarrierDto.Daily>> monoDailySeries = Mono.just(dto).map(CarrierDto::dailySeries);
        final Flux<CarrierDto.Daily> dailyFlux = monoDailySeries.flatMapMany(Flux::fromIterable);
        final Flux<Price> priceFlux = dailyFlux.map(daily -> new Price(daily.date(), daily.price()));

        priceFlux.subscribe(price -> System.out.println(String.format("Date: %s Price: %d", price.date(), price.price())));
    }
}

给出结果:

Using Transform function.
Date: 2023-01-01 Price: 100
Without transform function.
Date: 2023-01-01 Price: 100
Date: 2023-01-02 Price: 101
Date: 2023-01-03 Price: 102
Date: 2023-01-04 Price: 103

为什么结果不同?

java
  • 1 1 个回答
  • 20 Views

1 个回答

  • Voted
  1. Best Answer
    Igor Artamonov
    2023-10-25T03:15:43+08:002023-10-25T03:15:43+08:00

    这是因为transformerinMono生成一个新的Mono,即仅向发布者请求第一项。

    其内部有:

    Mono<V> transform(...) {
       ...
       from(transformer.apply(this))
    }
    

    我建议从 Flux 开始(或将 Mono 转换为 Flux)。就像你的例子一样:

    static Function<Flux<CarrierDto>, Flux<Price>> transform = carrierDtoFlux ->
            carrierDtoFlux.map(CarrierDto::dailySeries)
                    .flatMap(Flux::fromIterable)
                    .map(daily -> new Price(daily.date(), daily.price()));
    
    Flux.just(dto)
            .transform(transform)
            .subscribe(price -> System.out.println(String.format("Date: %s Price: %d", price.date(), price.price())));
    
    • 1

相关问题

  • Lock Condition.notify 抛出 java.lang.IllegalMonitorStateException

  • 多对一微服务响应未出现在邮递员中

  • 自定义 SpringBoot Bean 验证

  • Java 套接字是 FIFO 的吗?

  • 为什么不可能/不鼓励在服务器端定义请求超时?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve