如何禁用SimpleConsumer kafka 0.8.1的自动提交

时间:2016-12-06 00:50:19

标签: apache-kafka kafka-consumer-api

我想禁用kafka SimpleConsumer的自动提交。我使用的是0.8.1版本。对于高级消费者,可以通过consumerConfig设置和传递配置选项,如下所示 kafka.consumer.Consumer.createJavaConsumerConnector(this.consumerConfig);

如何为SimpleConsumer实现同样的目标?我主要想禁用自动提交。我尝试在consumer.properties中将auto commit设置为false并重新启动kafka服务器,zookeeper和producer。但是,这不起作用。我想我需要通过代码应用此设置,而不是在consumer.properties中。 任何人都可以帮忙吗?

以下是我的代码的样子

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));


如果上面的代码在提交偏移量之前崩溃,我仍然会在下一次运行中获得offsets.get(topicAndPartition).offset()的最新偏移量,这使我认为在执行代码时会发生偏移的自动提交。

1 个答案:

答案 0 :(得分:1)

使用SimpleConsumer只是意味着您需要处理有关消息消耗的所有内容,包括偏移提交,因此低级API不支持自动提交。

相关问题