Apache Kafka 1.0.0 Streams API Multiple Layered Group

How to use .groupby with few restrictions in the Kafka Streams API. Same as Java Streams API Example below

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());
}
+6
source share
1 answer

You specify a combination key by putting all the attributes / fields that you want to group into a key.

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

I just assumed that the country and the city are both String. You are using Interactive Queries to query the repository using

store.get("UK-London");

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

+3
source

All Articles