如何在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());
}
答案 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