Apache kafka K表值字段上的Kafka流分组

Apache kafka K表值字段上的Kafka流分组,apache-kafka,apache-kafka-streams,Apache Kafka,Apache Kafka Streams,我有一个用例,我的KTable是这样的 KTable:订单表 键:值 {123}:{id1,12} {124}:{id2,10} {125}:{id1,5} {126}:{id2,11} KTable:orderByIdTable=>此表将位于groupBy Value字段上,并且计数列值的和将为id1=(12+5),id2=(10+11) 键:值 {id1}:{17} {id2}:{21} final KTable<String, Order> orderTabl

我有一个用例,我的KTable是这样的

KTable:订单表

键:值

{123}:{id1,12}

{124}:{id2,10}

{125}:{id1,5}

{126}:{id2,11}

KTable
orderByIdTable
=>
此表将位于groupBy Value
字段上,并且计数列值的和将为
id1=(12+5)
id2=(10+11)

键:值

{id1}:{17}

{id2}:{21}

         final KTable<String, Order> orderTable = builder.table("order-topic");
         Don't know how to do this further.....
         final KTable<String,Long> orderByIdTable = ?
final KTable orderTable=builder.table(“订单主题”);
不知道如何进一步做到这一点。。。。。
最终KTable orderByIdTable=?
下面是一个代码示例(仅使用Java基元类型,这使我能够更快地组合起来),它演示了如何重新设置密钥,即重新划分KTable,从而生成新的KTable。您应该能够轻松地将其调整为将
KTable
转换为
KTable
的示例

就个人而言,我会为您的用例选择变体2

下面的例子未完全测试,可能是墓碑记录(具有非null键但具有null值的消息,指示应从表中删除该键)未正确处理

final StreamsBuilder builder=new StreamsBuilder();
final KTable table=builder.table(inputTopic,consumered.with(Serdes.Integer(),Serdes.String());
//变式1(https://docs.confluent.io/current/streams/faq.html#option-1-write-kstream-to-ak-read-back-as-ktable)
//在这里,我们重新设置KTable的键,将结果写入一个新主题,然后将该主题重新读取到一个新的KTable中。
桌子
.toStream()
.map((键,值)->KeyValue.pair(值,键))
.to(outputOPIc1,producted.with(Serdes.String(),Serdes.Integer());
K表更新表1=
table(outputTopic1,consumered.with(Serdes.String(),Serdes.Integer());
//变式2(https://docs.confluent.io/current/streams/faq.html#option-2-perform-a-dummy-aggregation)
//在这里,我们对KTable重新设置密钥(生成KGroupedTable),然后执行虚拟聚合以打开
//将KGroupedTable转换为KTable。
最终K表重新设置表2=
桌子
.群比(
(键,值)->KeyValue.pair(值,键),
已分组的.with(Serdes.String(),Serdes.Integer())
)
//虚拟聚合
.减少(
(aggValue,newValue)->newValue,/*加法器*/
(aggValue,oldValue)->oldValue/*减法器*/
);
rekeyedTable2.toStream().to(OutputOpic2,Producted.with(Serdes.String(),Serdes.Integer());

如何使用变量1对值的第二个字段求和?我看不出有任何方法可以引用旧值。对于这个用例来说,变体1听起来不必要。当您想要聚合时,应该使用
KTable.groupBy(…).aggregate(…)
(或
.reduce(…)
)。我个人会选择变体2。对于变体1,您的用例也可以工作,但是不需要往返到第二个卡夫卡主题,并且变体1与变体2相比会有一些开销。