如何获得kafka主题的最新偏移量?

时间:2016-07-18 03:31:55

标签: java apache-kafka kafka-consumer-api

我正在使用Java编写kafka个使用者。我想保留消息的实时,所以如果等待消费的消息太多,例如1000或者更多,我应该放弃未消耗的消息并开始使用最新的消息。

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

现在我的问题是如何获得一个主题的最新偏移,有人说我可以使用旧的消费者,但它太复杂,新的消费者是否有这个功能?

6 个答案:

答案 0 :(得分:21)

新消费者也很复杂。

if (manager.build.result == hudson.model.Result.UNSTABLE && manager.build.logFile.readLines().contains("Build step 'Record VS Code Metrics PowerTool Report' changed build result to UNSTABLE") { manager.build.@result = hudson.model.Result.SUCCESS }

//assign the topic consumer.assign();

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

答案 1 :(得分:8)

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

答案 2 :(得分:7)

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

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

答案 3 :(得分:2)

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

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
  }
}

答案 4 :(得分:1)

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;

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

答案 5 :(得分:0)

自kafka 1.0.1起,消费者使用一种名为endOffsets的方法

公共java.util.Map endOffsets(java.util.Collection分区)

请告诉我您是否需要完整的代码。

请参阅 apache-kafka-1.0.1-javadoc

相关问题