Apache Kafka 1.0.0 Streams API Multiple Multilevel groupby

Nir*_*ani 6 java apache-kafka apache-kafka-streams

我如何在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());
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*Sax 4

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

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

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

store.get("UK-London");
Run Code Online (Sandbox Code Playgroud)

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