Kafka获取最新offset

需求: 获取kafka 最新的offset

方法: 获取kafka每个分区的offset
method1 使用consumer.seektoend()
method2 使用consumer.endOffsets()

版本

     <dependency>
         <groupId>org.apache.kafka</groupId>
         <artifactId>kafka_2.11</artifactId>
         <version>2.0.0</version>
     </dependency>
package pers.nebo.kafkaoffset;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;

import java.util.*;

/**
 * @ author fnb
 * @ email nebofeng@gmail.com
 * @ date  2020/1/1
 * @ des : 获取kafka的最新offset
 */
public class KafkaGetLastOffset {

    public static void main(String[] args) {

        String topic="test-topic";

        Properties props = new Properties();
        props.put("bootstrap.servers", "node1:6667");
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        KafkaConsumer kafkaConsumer = new KafkaConsumer(props);
        List<TopicPartition> tps =new ArrayList<TopicPartition>();

        List<PartitionInfo> partitionInfoList=kafkaConsumer.partitionsFor("test");
        for(PartitionInfo partitionInfo:partitionInfoList){
            tps.add(new TopicPartition(topic,partitionInfo.partition()));
        }


        /*
         method1  参考博客做了一些修改,修改了认为不对的地方。
         https://www.cnblogs.com/cocowool/p/get_kafka_latest_offset.html
        */
       method1(topic,props,tps);

      /*
        method 2
        https://blog.csdn.net/chenggongdeli11/article/details/93502206
      */

       method2(topic,props,tps);
     }
    /**
     * by seek to end method
     * @param topic
     * @param props
     * @param tps
     */
     public static  void method1(String topic,Properties props,List <TopicPartition> tps){
        KafkaConsumer kafkaConsumer = new KafkaConsumer(props);
        kafkaConsumer.assign(Arrays.asList(topic));
        kafkaConsumer.seekToEnd(tps);
        for(TopicPartition topicPartition:tps){
            System.out.println("Partition " + topicPartition.partition()
                    + " 's latest offset is '" + kafkaConsumer.position(topicPartition));

        }

    }

    /**
     * by endoffsets method
     * @param topic
     * @param props
     * @param tps
     */
    public static void method2(String topic,Properties props,List <TopicPartition> tps) {
        KafkaConsumer kafkaConsumer = new KafkaConsumer(props);
        Map<TopicPartition,Long> topicPartitionOffsets=kafkaConsumer.endOffsets(tps);
        for(Map.Entry entry:topicPartitionOffsets.entrySet()){
            System.out.println(entry.getKey()+"=="+entry.getValue());

        }

    }
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值