我如何在 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());
}
最佳答案
您可以通过将要分组的所有属性/字段放入键中来指定组合键。
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
关于java - Apache Kafka 1.0.0 Streams API Multiple Multilevel groupby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48927093/