简体   繁体   English

Kafka 消费者没有分配到任何分区

[英]Kafka consumer assigned to no partitions

I am calling this class in a service which runs for every half an hour.我在每半小时运行一次的服务中调用此 class。 I just want to get the last message from a list of topic.我只想从主题列表中获取最后一条消息。 But randomly I am not getting the latest record.但随机我没有得到最新的记录。 Rather, it shows the offset value as 0 in that case.相反,它在这种情况下将偏移值显示为 0。 Also the newly assigned partitions are coming as empty Setting newly assigned partitions [].新分配的分区也将作为空设置新分配的分区[]。

 try {
            consumer.subscribe(Collections.singletonList(topic));

            ConsumerRecords last= consumer.poll(Duration.ofSeconds(2).toMillis());

            consumer.assignment().forEach(System.out::println);
            AtomicLong maxTimestamp = new AtomicLong();
            AtomicReference<ConsumerRecord<String, String>> latestRecord = new AtomicReference<>();

            // get the last offsets for each partition
            consumer.endOffsets(consumer.assignment()).forEach((topicPartition, offset) -> {
                logger.info("offset: "+offset);

                // seek to the last offset of each partition
                consumer.seek(topicPartition, (offset==0) ? offset:offset - 1);
                topicoffset=topicoffset+offset;
                // poll to get the last record in each partition
                consumer.poll(Duration.ofSeconds(10).toMillis()).forEach(record -> {
                    
                    // the latest record in the 'topic' is the one with the highest timestamp
                    if (record.timestamp() > maxTimestamp.get()) {
                        maxTimestamp.set(record.timestamp());
                        latestRecord.set(record);
                    }
                });
            });
            
            ConsumerRecord<String, String> lastrecord =latestRecord.get();

The consumer消费者

[Consumer clientId=consumer-1, groupId=get-offset-command-test-1] Discovered coordinator kafka.test.com:9092 (id: 2147483647 rack: null)
 [Consumer clientId=consumer-1, groupId=get-offset-command-test-1] Revoking previously assigned partitions []
 [Consumer clientId=consumer-1, groupId=get-offset-command-test-1] (Re-)joining group
 [Consumer clientId=consumer-1, groupId=get-offset-command-test-1] Successfully joined group with generation 2
[Consumer clientId=consumer-1, groupId=get-offset-command-test-1] Setting newly assigned partitions []

have two instances of the jar containing the above class having same group id... the topics are different but the group id is same... I get empty list only when both the instances are running.If i down one instance,then the other one is working fine.有两个 jar 实例,其中包含上述 class 具有相同的组 ID...主题不同但组 ID 相同...只有当两个实例都在运行时,我才会得到空列表。如果我关闭一个实例,那么另一个工作正常。

When you start the "other" instance to subscribe to a different topic, but in the same group, then the entire group will rebalance.当您启动“其他”实例以订阅不同的主题但在同一个组中时,整个组将重新平衡。 This will unassign (and eventually reassign) partitions to the "first instance".这将取消分配(并最终重新分配)分区到“第一个实例”。

It's unlikely both instances will have an empty list at the same time, but there is a possibility that one instance will grab all partitions from the group, but only for the topic it has been written to subscribe to.两个实例不太可能同时有一个空列表,但是一个实例可能会从组中获取所有分区,但仅针对它已写入订阅的主题。

When using the same group across consumer instances, best practice would be to always subscribe to the same topics.当跨消费者实例使用相同的组时,最佳实践是始终订阅相同的主题。

Since there's no clear reason to use the same group to get the "latest" record from any topic, I suggest using a random UUID for the group and then disabling any auto commits in the consumer config由于没有明确的理由使用同一组从任何主题获取“最新”记录,我建议为组使用随机 UUID,然后在使用者配置中禁用任何自动提交

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

相关问题 Kafka 消费者获得特定主题的分配分区 - Kafka Consumer get assigned partitions for a specific topic 获取分配给Kafka分区的使用者或客户端ID - Get the consumer or client id assigned to Kafka partitions 很少有 kafka 分区没有分配给任何 flink 使用者 - Few kafka partitions are not getting assigned to any flink consumer 多个分区上的 Kafka 消费者 - Kafka consumer on multiple partitions 在 Consumer 之间均匀分布 Kafka 分区 - Uniformly distribute the Kafka partitions among the Consumer Kafka:单消费者组,无分区多主题 - Kafka: Single consumer group, no partitions and multiple topics Kafka使用者 - 消费者进程和线程与主题分区的关系是什么 - Kafka consumer - what's the relation of consumer processes and threads with topic partitions Apache Kafka:3个分区,消费组有3个消费者,每个消费者都应该是多线程的 - Apache Kafka: 3 partitions, 3 consumers in the consumer group, each consumer should be multithreaded 根据 Consumer.committablePartitionedSource 中分配的分区数调整并行度 - Adjusting parallism based on number of partitions assigned in Consumer.committablePartitionedSource Kafka 消费者偏移超出范围,没有为分区配置重置策略 - Kafka consumer offsets out of range with no configured reset policy for partitions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM