关于怎么获取kafka指定位置offset消息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于怎么获取kafka指定位置offset消息相关的知识,希望对你有一定的参考价值。
参考技术A For finding the start offset to read in Kafka 0.8 Simple Consumer example they sayKafka includes two constants to help,
kafka.api.OffsetRequest.EarliestTime() finds the beginning of the data
in the logs and starts streaming from there,
kafka.api.OffsetRequest.LatestTime() will only stream new messages.
You can also find the example code there for managing the offset at your consumer end.
public static long getLastOffset(SimpleConsumer consumer, String topic, int partition,
long whichTime, String clientName)
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(requestInfo, kafka.api.OffsetRequest.CurrentVersion(),clientName);
OffsetResponse response = consumer.getOffsetsBefore(request);
if (response.hasError())
System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition) );
return 0;
long[] offsets = response.offsets(topic, partition);
return offsets[0];
本回答被提问者采纳
以上是关于关于怎么获取kafka指定位置offset消息的主要内容,如果未能解决你的问题,请参考以下文章
每条消息在分区中的位置信息由一个叫offset的数据来表征。