如何将GroupBy结果列表添加到Webflux中的新Map

问题描述 投票:0回答:1

如何将GroupBy结果列表制作到Webflux中的新Map

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

// 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个回答
0
投票

我知道了。

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!!
    }
© www.soinside.com 2019 - 2024. All rights reserved.