我希望每条消息将许多记录变成一个。我尝试了许多类似自定义归约和聚合器的方法,但是它们仍然可以发送一对一的记录。例如,我想将许多字符串转换为一个字符串。如果我的信息流中的消息具有相同的键,但是值不同,则分别是“ the”,“ sky”,“ is”,“ blue”,那么我想在新主题“ the,sky,是蓝色。”相反,我得到的是4条消息“ the”,“ the,sky”,“ the,sky,is”,“ the,sky,is,blue”。当我向kafka用户发送第二条消息时,它将连接到先前的聚合上,最终我收到此消息“ the,sky,is,blue,the,sky,is,blue,”
我还尝试使用自定义storebuilder并更改许多设置以查看是否可以执行任何操作。
Map<String, String> changelogConfig = new HashMap<>();
changelogConfig.put("message.down.conversion.enable", "true");
changelogConfig.put("flush.messages", "0");
changelogConfig.put("flush.ms", "0");
StoreBuilder<KeyValueStore<String, String>> aggStoreSupplier = Stores.keyValueStoreBuilder(
Stores.persistentKeyValueStore("AggStore"),
Serdes.String(),
Serdes.String())
.withLoggingEnabled(changelogConfig);
KStream<String, String> results = source // single message get processed and eventually i get these string results I need to concatenate
.groupByKey() // this kgroupedstream has the N records, which was how many were sent in the message
.reduce(new Reducer<String>() {
@Override
public String apply(String aggValue, String value) {
return value + "," + aggValue;
}
}, Materialized.as("AggStore"))
.toStream();
results.to("results", Produced.with(Serdes.String(), Serdes.String()));
final Topology topology = builder.build(); // to describe topology
System.out.println(topology.describe()); // to print description
final KafkaStreams streams = new KafkaStreams(topology, props);
final CountDownLatch latch = new CountDownLatch(1);
// attach shutdown handler to catch control-c
Runtime.getRuntime().addShutdownHook(new Thread("streams-shutdown-hook") {
@Override
public void run() {
streams.close();
latch.countDown();
}
});
try {
streams.cleanUp();
streams.start();
latch.await();
} catch (Throwable e) {
System.exit(1);
}
System.exit(0);