[英]Kafka manage offset between consumer group
假设我在2个Kafka分区中有一个topic(test),并且每个消费者组中只有一个消费者的2个消费者组(X,Y)正在消费该主题。
现在,我想知道同一分区中另一个使用者组的偏移量。 下面的伪代码将解释需要
*** Let's assume this is running in the context of consumer group X
TOPIC = “test”
// consumer for group x
Consumer<K, V> consumerX = new KafkaConsumer<>(consumerProperties);
consumerX.subscribe(TOPIC, new ReportOnRebalance(……..));
// Get the current assigned partition, could be null but keep searching
// until partition got assigned to the consumerX
Set<TopicPartition> topicPartition = consumerX.assignment();
// Get the last committed offset
offsetAndMetadataX = consumerX.committed(topicPartition)
// consumer for group y
Consumer<K, V> consumerY = new KafkaConsumer<>(consumerProperties);
// manually assign because I am interested in the offset for the
// partition consumerX is going to serve for
consumerY.assign(topicPartition)
// Get the last committed offset
offsetAndMetadataY = consumerY.committed(topicPartition)
// Do require logic with offsetAndMetadataC and offsetAndMetadataY
newOffset = foo(offsetAndMetadataX, offsetAndMetadataY)
// want to reset the offset for this consumerY and in this
// partition
consumerY.seek(topicPartition, bar(newOffset))
// Change offset for consumerX and starting polling for messages
consumerX.seek(topicPartition, newOffset)
while(...) {
consumerX.poll(..)
....
}
*** Now the same code will run in the context of consumer group Y, but the role will be reversed
consumerY.subscribe()
consumerX.assign()
...
consumerY.seek(topicPartition, bar(newOffset))
...
// Change offset for consumerY and starting polling for messages
consumerY.seek(topicPartition, newOffset)
while(...) {
consumerY.poll(..)
....
}
我不确定上述逻辑是否有效。 我不确定的部分是当一个使用者组(X)在一台机器上确实订阅时,假设分区(1)被分配而同一使用者组(X)在另一台机器上进行了分配,但也确实寻求作为分配的一部分有所抵消。 我不知道这行不通吗?
为什么要执行此操作,想要了解分配和订阅的使用方式,我们还需要手动跳过处理其他使用者组已经处理过的一些偏移量,或者重新处理其他使用者已经处理过的旧偏移量
我没有尝试您在这里描述的内容,但是从官方文档看来,这应该可以按照您希望的那样工作:
此处突出显示的关键部分:
手动分区分配不使用组协调,因此使用者故障不会导致重新分配分配的分区。 即使每个使用者与另一个使用者共享一个groupId,每个使用者也独立执行操作。 为了避免偏移提交冲突,通常应确保groupId对于每个使用者实例都是唯一的。
基本上,如果您开始手动将分区分配给使用者,则似乎所有动态重新平衡都会自动关闭。 因此,您应该小心,但似乎Kafka确实允许您描述的方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.