kafka\u consumer\u offsets主题的分区计数过多

ijxebb2r  于 2021-06-07  发布在  Kafka
关注(0)|答案(2)|浏览(363)

我使用的是kafka 0.8.2,我的消费者中出现了一个错误,说“offset commit failed with…”。在查看“消费者补偿”主题时。我看到它有50个分区。这正常吗?我只能通过删除所有Kafka日志并重新启动我的Kafka服务器来解决这个问题。当这个主题达到一定数量的分区时,有没有办法删除它,或者我提交的偏移量有误?
以下是我提交偏移量的方式:

public void commitOffsets(BlockingChannel channel, String topic, String    groupid, int partition, String clientName, int corrilationid, long offset)   throws Exception{

    if (commitTryCount > 100){
        throw new Exception("Offset commit failed with " + channel.host());
    }

    long now = System.currentTimeMillis();
    Map<TopicAndPartition, OffsetAndMetadata> offsets = new LinkedHashMap<TopicAndPartition, OffsetAndMetadata>();
    //for (int i = 0; i < this.totalPartitions; i++){
        TopicAndPartition topicPartition = new TopicAndPartition(topic, partition);
        offsets.put(topicPartition, new OffsetAndMetadata(offset, topic, now));
    //}     

    //initialize offset commit
    OffsetCommitRequest commitRequest = new OffsetCommitRequest(groupid, offsets, corrilationid, clientName, (short) 1);
    channel.send(commitRequest.underlying());
    OffsetCommitResponse commitResponse = OffsetCommitResponse.readFrom(channel.receive().buffer());
    if (commitResponse.hasError()){         
        for (Object partitionErrorCode: commitResponse.errors().values()){
            if (Short.parseShort(partitionErrorCode.toString()) == ErrorMapping.OffsetMetadataTooLargeCode()){
                //reduce the size of the metadata and retry
                offset--;
                commitOffsets(channel, topic, groupid, partition, clientName, corrilationid, offset);
                commitTryCount++;
            } else if (Short.parseShort(partitionErrorCode.toString()) == ErrorMapping.NotCoordinatorForConsumerCode()
                    || Short.parseShort(partitionErrorCode.toString()) == ErrorMapping.ConsumerCoordinatorNotAvailableCode()) {
                //discover new coordinator and retry
                int newCorrilation = corrilationid;
                newCorrilation++;
                this.channel = discoverChannel(channel.host(), port, groupid, clientName, newCorrilation);
                commitOffsets(this.channel, topic, groupid, partition, clientName, newCorrilation, offset);
                commitTryCount++;
            } else{
                //retry
                commitOffsets(channel, topic, groupid, partition, clientName, corrilationid, offset);
                commitTryCount++;
            }//end of else              
        }//end of for
    }//end of if
}//end of method
dohp0rv5

dohp0rv51#

我把密码贴出来后就知道了。当提交成功时,我忘了将变量“committrycount”设置为0。我还在想,消费者偏移量主题有50个分区是否正常?

ct2axkht

ct2axkht2#

是的,50个分区用于消费偏移是默认值。要更改,请设置 offsets.topic.num.partitions 财产。

相关问题