我有一个由 byte[] 键控的主题,我想对其进行重新分区并通过消息正文中某个字段中的另一个键来处理该主题。
我发现有 KGroupedStream
和 groupby
函数。但它要求聚合函数转换为 KTable/KStream。我不需要聚合。我只想重新分区并处理输出。
最佳答案
是的,你可以。您设置一个新 key ,然后通过另一个主题传输数据。
// repartition() will create the required topic automatically for your,
// with the same number of partitions as your input topic;
//
// it's also possible to set the number of partitions explicitly to scale in/out
// via `repartitioned(Repartitioned.numberOfPartitions(...))`
KStream stream = ...
KStream repartionedStream = stream.selectKey(...)
.repartition();
// older versions:
//
// using `through()` you need to create the use topic manually,
// before you start your application
KStream stream = ...
KStream repartionedStream = stream.selectKey(...)
.through("topic-name");
请注意,在启动具有所需数量分区的应用程序之前,您需要创建在 through()
中使用的主题。
关于apache-kafka - 有没有办法对 Kafka 流中的输入主题进行重新分区?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49578931/