Kafka AdminClient 管理Kafka Offset代码实现

Kafka AdminClient 管理Kafka Offset代码实现

一:offset保存位置

     kafka的offset管理,可以交给zookeeper实现,经过配置offsets.storage=zookeeper;也可以直接跟kafka自己管理,在你 的配置文件设置下面的配置项;

offsets.storage=kafka 

dual.commit.enabled=true 

kafka自己管理自己的offset,并经过命令可以查看

bin/kafka-consumer-groups.sh --bootstrap-server broker1:9092 --describe --group kafkatest


 如上图可以查看到group中每个消费者对应topic的patition,当前消费的offset;每个partition中最大的offset等信息;经过测试,当没有消费者进行消费时候,若执行上面命令会出现下面的现象:消费者不见了


以上就是用kafka命令获取的结果;

下面是经过查看借鉴源码,用Java API实现的offset管理(kafka 版本为0.10)

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka_2.10</artifactId>
    <version>0.10.2.1</version>
</dependency>
<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>0.10.2.1</version>
</dependency>

测试类代码如下:

[java]  view plain  copy
  1. public static void main(String[] args) throws InstantiationException, IllegalAccessException {  
  2.       Properties props = new Properties();  
  3.       props.put(CommonClientConfigs.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");  
  4.       AdminClient adminClient = AdminClient.create(props);  
  5.       ConsumerGroupSummary  consumerGroupSummary =  adminClient.describeConsumerGroup("kafkatest");  
  6.       if(consumerGroupSummary.state().equals("Empty")){  
  7.           System.out.println("niaho");  
  8.       }  
  9.       Option<List<ConsumerSummary>> consumerSummaryOption =  consumerGroupSummary.consumers();  
  10.   
  11.       List<ConsumerSummary> ConsumerSummarys = consumerSummaryOption.get();//获取组中的消费者  
  12.       KafkaConsumer consumer = getNewConsumer();  
  13.       for(int i=0;i<ConsumerSummarys.size();i++){ //循环组中的每一个消费者  
  14.             
  15.           ConsumerSummary consumerSummary = ConsumerSummarys.apply(i);  
  16.           String consumerId  = consumerSummary.consumerId();//获取消费者的id  
  17.           scala.collection.immutable.Map<TopicPartition, Object> maps =   
  18.                   adminClient.listGroupOffsets("kafkatest");//或者这个组消费的所有topic,partition和当前消费到的offset  
  19.           List<TopicPartition> topicPartitions= consumerSummary.assignment();//获取这个消费者下面的所有topic和partion  
  20.           for(int j =0;j< topicPartitions.size();j++){ //循环获取每一个topic和partion  
  21.               TopicPartition topicPartition = topicPartitions.apply(j);  
  22.               String CURRENToFFSET = maps.get(topicPartition).get().toString();  
  23.               long endOffset =getLogEndOffset(topicPartition);  
  24.               System.out.println("topic的名字为:"+topicPartition.topic()+"====分区为:"+topicPartition.partition()+"===目前消费offset为:"+CURRENToFFSET+"===,此分区最后offset为:"+endOffset);  
  25.           }  
  26.       }  
  27. }  
引用的两个方法代码如下:

获取消费者

[java]  view plain  copy
  1. public static KafkaConsumer getNewConsumer(){  
  2.          Properties props = new Properties();  
  3.          props.put("bootstrap.servers""localhost:9092");  
  4.          props.put("group.id""kafkatest");  
  5.          props.put("enable.auto.commit""true");  
  6.          props.put("auto.offset.reset""earliest");  
  7.          props.put("auto.commit.interval.ms""1000");  
  8.          props.put("auto.commit.interval.ms""1000");  
  9.          props.put("key.deserializer""org.apache.kafka.common.serialization.StringDeserializer");  
  10.          props.put("value.deserializer""org.apache.kafka.common.serialization.StringDeserializer");  
  11.          KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);  
  12.          return consumer;  
  13.   }  
获取最后的offset:

[java]  view plain  copy
  1. public static long getLogEndOffset(TopicPartition topicPartition){  
  2.       KafkaConsumer<String, String> consumer= getNewConsumer();  
  3.       consumer.assign(Arrays.asList(topicPartition));  
  4.       consumer.seekToEnd(Arrays.asList(topicPartition));  
  5.       long endOffset = consumer.position(topicPartition);  
  6.       return endOffset;  
  7.   }  
运行后,可以获取结果如下:
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值