Apache Kafka 1.0.0 Streams API Multiple Multilevel groupby

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

如何在Kafka Streams API中使用带有多个约束的.groupby。与下面的Java 8 Streams API示例相同

public void twoLevelGrouping(List<Person> persons) {
     final Map<String, Map<String, List<Person>>> personsByCountryAndCity = persons.stream().collect(
         groupingBy(Person::getCountry,
            groupingBy(Person::getCity)
        )
    );
    System.out.println("Persons living in London: " + personsByCountryAndCity.get("UK").get("London").size());
}
apache apache-kafka apache-kafka-streams confluent-kafka confluent
1个回答
3
投票

您可以通过将要分组的所有属性/字段放入键中来指定组合键。

KTable table = stream.selectKey((k, v,) -> k::getCountry + "-" + k::getCity)
                     .groupByKey()
                     .aggregate(...); // or maybe .reduce()

我只是假设国家和城市都是String。您使用交互式查询来查询商店

store.get("UK-London");

https://docs.confluent.io/current/streams/developer-guide/interactive-queries.html

© www.soinside.com 2019 - 2024. All rights reserved.