如何获取kafka主题分区的last/end偏移量?

How can I get the last/end offset of a kafka topic partition?

我正在使用 Java 编写一个 kafka 消费者。我想保持消息的实时性,所以如果等待消费的消息过多,比如1000条或更多,我应该放弃未消费的消息,从最后一个偏移量开始消费。

对于这个问题,我尝试比较一个主题(只有1个分区)的最后一次提交的偏移量和结束偏移量,如果这两个偏移量之间的差异大于一定量,我将设置最后一次提交主题的偏移量作为下一个偏移量,以便我可以放弃那些多余的消息。

现在我的问题是如何获取topic的end offset,有人说可以用old consumer,但是太复杂了,new consumer有这个功能吗?

新消费者也很复杂

//assign the topic consumer.assign();

//seek to end of the topic consumer.seekToEnd();

//the position is the latest offset consumer.position();

Kafka 版本:0.10.1.1

// Get the diff of current position and latest offset
Set<TopicPartition> partitions = new HashSet<TopicPartition>();
TopicPartition actualTopicPartition = new TopicPartition(record.topic(), record.partition());
partitions.add(actualTopicPartition);
Long actualEndOffset = this.consumer.endOffsets(partitions).get(actualTopicPartition);
long actualPosition = consumer.position(actualTopicPartition);          
System.out.println(String.format("diff: %s   (actualEndOffset:%s; actualPosition=%s)", actualEndOffset -actualPosition ,actualEndOffset, actualPosition));  
KafkaConsumer<String, String> consumer = ...
consumer.subscribe(Collections.singletonList(topic));
TopicPartition topicPartition = new TopicPartition(topic, partition);
consumer.poll(0);
consumer.seekToEnd(Collections.singletonList(topicPartition));
long currentOffset = consumer.position(topicPartition) -1;

以上代码段 returns 给定主题和分区号的当前已提交消息偏移量。

您还可以使用kafka服务器命令行工具:

./bin/kafka-run-class.sh kafka.tools.GetOffsetShell --broker-list localhost:9092 --topic topic-name

输出格式为<topicName>:<partitionID>:<offset>,例如t1:0:0, 见 https://jaceklaskowski.gitbooks.io/apache-kafka/kafka-tools-GetOffsetShell.html 了解更多详情。

我开发了以下代码来获取偏移量状态

import java.util
import java.util.{Collections, Properties}

import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.common.{PartitionInfo, TopicPartition}
import org.apache.kafka.common.serialization.StringDeserializer
import scala.collection.JavaConverters._

class GetOffsetRange(consumer:KafkaConsumer[String,String]) {

  def getStartOffsetRange(topic:String):util.HashMap[TopicPartition,Long]={

    val topicPartitionList = consumer.partitionsFor(topic)
    val partitionMap=new util.HashMap[TopicPartition,Long]()
    val arrTopic=new util.ArrayList[TopicPartition]()

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

    for(topic<-topicPartitionList.asScala){
      println(topic.topic() +","+topic.partition())
      arrTopic.add(new TopicPartition(topic.topic(),topic.partition()))
    }

    consumer.poll(0)

    consumer.seekToBeginning(arrTopic)

    for(partition <- arrTopic.asScala){
      partitionMap.put(partition,consumer.position(partition)-1)
    }
    return partitionMap
  }

  def getEndOffsetRange(topic:String):util.HashMap[TopicPartition,Long]={

    val topicPartitionList = consumer.partitionsFor(topic)
    val partitionMap=new util.HashMap[TopicPartition,Long]()
    val arrTopic=new util.ArrayList[TopicPartition]()

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

    for(topic<-topicPartitionList.asScala){
      println(topic.topic() +","+topic.partition())
      arrTopic.add(new TopicPartition(topic.topic(),topic.partition()))
    }

    consumer.poll(0)

    consumer.seekToEnd(arrTopic)

    for(partition <- arrTopic.asScala){
      partitionMap.put(partition,consumer.position(partition)-1)
    }
    return partitionMap
  }
}

从 kafka 1.0.1 开始,消费者有一个名为 endOffsets 的方法

public java.util.Map endOffsets(java.util.Collection 个分区)

如果您需要完整代码,请告诉我..

请参考 apache-kafka-1.0.1-javadoc