有没有办法对 Kafka 流中的输入主题进行重新分区?
Is there a way to repartition the input topic in Kafka streams?
我有一个由 byte[] 键控的主题,我想对其进行重新分区并通过消息正文字段中的另一个键处理该主题。
我发现有 KGroupedStream
和 groupby
函数。但它要求聚合函数转换为 KTable/KStream。我不需要聚合。我只想重新分区并处理输出。
是的,你可以。您设置了一个新密钥,然后通过另一个主题传输数据。
// 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()
中使用的主题。
(Kafka Streams 2.5.x 或更早版本)
不确定这是否完全符合犹太洁食标准,但它确实有效,并且重新分区主题是自动创建的,并且具有正确数量的分区 wrt stream
。
KTable emptyTable = someTable.filter((k, v) -> false);
KStream stream = ...
KStream repartionedStream = stream.selectKey(...)
.leftJoin(emptyTable, (v, Null) -> v, ...);
编辑
在 2020 年 8 月引入 Kafka Streams 2.6.0 并且 KStream.repartition() 应运而生时,这种方法显然成为了一种复杂的憎恶行为,值得大量的反对票和鞭笞。
所以对于流版本 2.6.x+ 你必须使用
KStream stream = ...
KStream repartionedStream = stream.selectKey(...)
.repartition();
KStream 接口上有一个 repartition() 方法,允许您根据 Serdes 和 StreamPartitioner 对主题进行重新分区,而不是 mapping/selectingKey() 加上应用直通或重新分区。
我有一个由 byte[] 键控的主题,我想对其进行重新分区并通过消息正文字段中的另一个键处理该主题。
我发现有 KGroupedStream
和 groupby
函数。但它要求聚合函数转换为 KTable/KStream。我不需要聚合。我只想重新分区并处理输出。
是的,你可以。您设置了一个新密钥,然后通过另一个主题传输数据。
// 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()
中使用的主题。
(Kafka Streams 2.5.x 或更早版本)
不确定这是否完全符合犹太洁食标准,但它确实有效,并且重新分区主题是自动创建的,并且具有正确数量的分区 wrt stream
。
KTable emptyTable = someTable.filter((k, v) -> false);
KStream stream = ...
KStream repartionedStream = stream.selectKey(...)
.leftJoin(emptyTable, (v, Null) -> v, ...);
编辑
在 2020 年 8 月引入 Kafka Streams 2.6.0 并且 KStream.repartition() 应运而生时,这种方法显然成为了一种复杂的憎恶行为,值得大量的反对票和鞭笞。
所以对于流版本 2.6.x+ 你必须使用
KStream stream = ...
KStream repartionedStream = stream.selectKey(...)
.repartition();
KStream 接口上有一个 repartition() 方法,允许您根据 Serdes 和 StreamPartitioner 对主题进行重新分区,而不是 mapping/selectingKey() 加上应用直通或重新分区。