简体   繁体   English

如何在 JS 中为 Kafka 消费者获取分配的分区

[英]How to get Assigned Partition for a Kafka Consumer in JS

I'm working with a Kafka Consumer using KAFKAJS.我正在使用 KAFKAJS 与 Kafka 消费者合作。 I have a requirement to know all the partitions assigned to a specific consumer.我需要知道分配给特定消费者的所有分区。

For example, let's say Topic 1 has 5 partitions.例如,假设主题 1 有 5 个分区。 And there are 2 consumers with the same clientId.并且有 2 个消费者具有相同的 clientId。 So, one will be assigned 3 topics and the other 2. I want each consumer to know the partitions assigned.因此,将为一个分配 3 个主题,另一个分配 2 个主题。我希望每个消费者都知道分配的分区。

We can use consumer.describeGroup to query the state of the consumer group.我们可以使用consumer.describeGroup查询消费者组的state。 https://kafka.js.org/docs/consuming#a-name-describe-group-a-describe-group https://kafka.js.org/docs/sumption#a-name-describe-group-a-describe-group

The memberAssignment field describes what topic-partitions are assigned to each member, but it's a Buffer, so you'll need to decode that using AssignerProtocol.MemberAssignment.decode: memberAssignment 字段描述了分配给每个成员的主题分区,但它是一个缓冲区,因此您需要使用 AssignerProtocol.MemberAssignment.decode 对其进行解码:

https://github.com/tulios/kafkajs/blob/master/index.js#L18 https://github.com/tulios/kafkajs/blob/master/index.js#L18

We can also listen to the GROUP_JOIN event.我们也可以监听 GROUP_JOIN 事件。 It already contains the member assignment, so you don't need to actually describeGroup:它已经包含成员分配,因此您不需要实际描述Group:

https://kafka.js.org/docs/instrumentation-events#consumer https://kafka.js.org/docs/instrumentation-events#consumer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM