Apache Kafka 1.0.0 Streams API Multiple Multilevel groupby

时间:2018-02-22 12:11:32

标签: apache apache-kafka apache-kafka-streams confluent-kafka confluent

如何在Kafka Streams API中使用.groupby和Multiple constrainst。与下面的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());
}

1 个答案:

答案 0 :(得分: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