【问题标题】:How to make GroupBy result list to new Map in Webflux如何在 Webflux 中将 GroupBy 结果列表添加到新地图
【发布时间】:2020-07-04 17:57:08
【问题描述】:

如何在 Webflux 中将 GroupBy 结果列表添加到新地图

有我的输入列表和期望结果。那我该怎么做才能得到结果。

// expect
{
    "timestamp": "2019-06-13T00:00:00.000",
    "result": {
        "first": 1,
        "second": 2,
        "third": 3
    }
}

// input list
[
    {
        "timestamp": "2019-06-13T00:00:00.000",
        "first": 1
    },
    {
        "timestamp": "2019-06-13T00:00:00.000",
        "second": 2
    },
    {
        "timestamp": "2019-06-13T00:00:00.000",
        "third": 3
    }
]

val Flux.fromIterable(list)
   .groupBy{it.timestamp}
   .concatMap { groupItem ->

     // here!! I want to make `group by result list to new Map``
     Result(timestamp = groupItem.key()!!, Item(first = ?, second = ?, third =?))
   }

【问题讨论】:

    标签: kotlin flux reactor


    【解决方案1】:

    我想通了。

    Flux.merge(first, second, thrid)
        .groupBy { it.timestamp }
        .concatMap {
            it.map { item ->
                val container = mutableMapOf<String, Any>()
                if (item is firstEntity) {
                    container["first"] = item.result.count
                    container["timestamp"] = it.key()!!
                }
                if (item is secondEntity) container["second"] = item.result.count
                if (item is thridEntity) container["thrid"] = item.result.count
                container
            }.reduce { acc, current ->
                acc.putAll(current)
                acc
            }
        }
        .map {
            val first = (it["first"] ?: 0) as Int
            val second = (it["second"] ?: 0) as Int
            val thrid = (it["thrid"] ?: 0) as Int
            val timestamp = (it["timestamp"] ?: "") as String
    
            // somthing!!
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2016-09-01
      • 2018-12-12
      • 2022-08-11
      • 2021-04-09
      • 2023-02-03
      • 2021-10-31
      相关资源
      最近更新 更多