Apache kafka 如何禁用SimpleConsumer kafka 0.8.1的自动提交
我想禁用kafka SimpleConsumer的自动提交。我使用的是0.8.1版本。对于高级使用者,可以通过consumerConfig设置和传递配置选项,如下所示 kafka.consumer.consumer.createJavaConsumerConnectionr(this.consumerConfig) 如何为SimpleConsumer实现同样的功能?我主要想禁用自动提交。我尝试在consumer.properties中将自动提交设置为false,并重新启动了kafka服务器、zookeeper和producer。但是,这是行不通的。我想我需要通过代码而不是在consumer.properties中应用此设置。 有人能帮忙吗 下面是我的代码的样子Apache kafka 如何禁用SimpleConsumer kafka 0.8.1的自动提交,apache-kafka,kafka-consumer-api,Apache Kafka,Kafka Consumer Api,我想禁用kafka SimpleConsumer的自动提交。我使用的是0.8.1版本。对于高级使用者,可以通过consumerConfig设置和传递配置选项,如下所示 kafka.consumer.consumer.createJavaConsumerConnectionr(this.consumerConfig) 如何为SimpleConsumer实现同样的功能?我主要想禁用自动提交。我尝试在consumer.properties中将自动提交设置为false,并重新启动了kafka服务器、zo
List<TopicAndPartition> topicAndPartitionList = new ArrayList<>();
topicAndPartitionList.add(topicAndPartition);
OffsetFetchResponse offsetFetchResponse = consumer.fetchOffsets(new OffsetFetchRequest("testGroup", topicAndPartitionList, (short) 0, correlationId, clientName));
Map<TopicAndPartition, OffsetMetadataAndError> offsets = offsetFetchResponse.offsets();
FetchRequest req = new FetchRequestBuilder()
.clientId(clientName)
.addFetch(a_topic, a_partition, offsets.get(topicAndPartition).offset(), 100000) .build();
long readOffset = offsets.get(topicAndPartition).offset();
FetchResponse fetchResponse = consumer.fetch(req);
//Consume messages from fetchResponse
Map<TopicAndPartition, OffsetMetadataAndError > requestInfo = new HashMap<> ();
requestInfo.put(topicAndPartition, new OffsetMetadataAndError(readOffset, "metadata", (short)0));
OffsetCommitResponse offsetCommitResponse = consumer.commitOffsets(new OffsetCommitRequest("testGroup", requestInfo, (short)0, correlationId, clientName));
List-topicAndPartitionList=new-ArrayList();
topicAndPartitionList.add(topicAndPartition);
OffsetFetchResponse OffsetFetchResponse=consumer.fetchOffset(新的OffsetFetchRequest(“testGroup”,topicAndPartitionList,(short)0,correlationId,clientName));
Map offsets=offsetFetchResponse.offsets();
FetchRequest req=新的FetchRequestBuilder()
.clientId(clientName)
.addFetch(一个主题,一个分区,offset.get(topicAndPartition).offset(),100000.build();
long readOffset=offset.get(topicAndPartition.offset();
FetchResponse FetchResponse=consumer.fetch(req);
//使用fetchResponse中的消息
Map requestInfo=newhashmap();
put(topicAndPartition,新的OffsetMetadataAndError(readOffset,“metadata”,(short)0));
OffsetCommitResponse OffsetCommitResponse=consumer.commitofset(新的OffsetCommitRequest(“测试组”,requestInfo,(短)0,correlationId,clientName));
如果上面的代码在提交偏移量之前崩溃,我仍然会在下一次运行中得到offset.get(topicAndPartition).offset()的最新偏移量,这让我认为自动提交偏移量是在代码执行时发生的。使用SimpleConsumer只意味着您希望处理有关消息消费的一切,包括偏移量提交,因此,低级API不支持自动提交。我在上面添加了代码片段。如果我的应用程序在提交偏移量之前崩溃,我仍然会在下一次运行中获得最新的偏移量。因此,我认为如果未显式禁用自动提交,则自动提交会在内部发生。这是因为配置“auto.offset.reset”默认设置为最大,这意味着如果Zookeeper上未存储初始偏移量,则使用者会将其偏移量重置为最大偏移量(版本0始终存储到Zookeeper)即使在之前提交偏移量成功时多次运行,我也看到了相同的行为。CommitteOffsets不会写入zookeeper吗?是的,它会将偏移提交给zookeeper。请检查zookeeper znode'/consumers//offset//以查看是否已成功提交偏移量。