java监控kafka获取 logSize offset lag

<!-- kafka 监控Api  排除slf4j-log4j12  防止报错 V    注意和spring-kafka的版本兼容-->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>3.4.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

在这里插入图片描述

package com.orientalsemi.oceanplus.dataetl.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.clients.consumer.OffsetAndMetadata;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;

import java.util.*;

@Slf4j
public class KafkaUtil {

    public static long KafkaConsumeLag(String server, String groupId, String topic){
        Map endOffsetMap = new HashMap();
        Map commitOffsetMap = new HashMap();
        Properties consumeProps = getConsumeProperties(groupId, server);
        log.info("consumer properties:" + consumeProps);
        //查询topic partitions
        KafkaConsumer consumer = new KafkaConsumer(consumeProps);
        List topicPartitions = new ArrayList();
        List partitionsFor = consumer.partitionsFor(topic);
        for (Object map : partitionsFor) {
            PartitionInfo partitionInfo = (PartitionInfo) map;
            TopicPartition topicPartition = new TopicPartition(partitionInfo.topic(), partitionInfo.partition());
            topicPartitions.add(topicPartition);
        }
        //查询logsize
        Map endOffsets = consumer.endOffsets(topicPartitions);
        for (Object map : endOffsets.keySet()) {
            TopicPartition partitionInfo = (TopicPartition) map;
            endOffsetMap.put(partitionInfo.partition(), endOffsets.get(partitionInfo));
        }
        for (Object partitionId : endOffsetMap.keySet()) {
            log.info(String.format("at %s, topic:%s, partition:%s, logSize:%s", System.currentTimeMillis(), topic, partitionId, endOffsetMap.get(partitionId)));
        }
        //查询消费offset
        for (Object map : topicPartitions) {
            TopicPartition topicAndPartition = (TopicPartition) map;
            OffsetAndMetadata committed = consumer.committed(topicAndPartition);
            commitOffsetMap.put(topicAndPartition.partition(), committed.offset());
        }
        //累加lag
        long lagSum = 0l;
        if (endOffsetMap.size() == commitOffsetMap.size()) {
            for (Object data : endOffsetMap.keySet()) {
                long endOffSet = (long) endOffsetMap.get(data);
                long commitOffSet = (long) commitOffsetMap.get(data);
                long diffOffset = endOffSet - commitOffSet;
                lagSum += diffOffset;
                log.info("Topic:" + topic + ", groupID:" + groupId + ", partition:" + data + ", endOffset:" + endOffSet + ", commitOffset:" + commitOffSet + ", diffOffset:" + diffOffset);
            }
            log.info("Topic:" + topic + ", groupID:" + groupId + ", LAG:" + lagSum);
        } else {
            log.info("this topic partitions lost");
        }
        consumer.close();
        return lagSum;
    }



    public static Properties getConsumeProperties(String groupID, String bootstrap_server) {
        Properties props = new Properties();
        props.put("group.id", groupID);
        props.put("bootstrap.servers", bootstrap_server);
        props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
        return props;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
获取Kafka的topic的offset,可以使用KafkaConsumer类中的方法。以下是Java代码示例: ```java import org.apache.kafka.clients.consumer.ConsumerConfig; import org.apache.kafka.clients.consumer.ConsumerRecord; import org.apache.kafka.clients.consumer.ConsumerRecords; import org.apache.kafka.clients.consumer.KafkaConsumer; import org.apache.kafka.common.TopicPartition; import java.time.Duration; import java.util.ArrayList; import java.util.List; import java.util.Properties; public class KafkaOffsetGetter { public static void main(String[] args) { // Kafka consumer configuration Properties props = new Properties(); props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group"); props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); // Create Kafka consumer KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props); // Assign topic partitions List<TopicPartition> partitions = new ArrayList<>(); TopicPartition partition0 = new TopicPartition("my-topic", 0); TopicPartition partition1 = new TopicPartition("my-topic", 1); partitions.add(partition0); partitions.add(partition1); consumer.assign(partitions); // Seek to beginning of topic consumer.seekToBeginning(partitions); // Print offset for each partition while (true) { ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100)); for (ConsumerRecord<String, String> record : records) { System.out.println("Partition: " + record.partition() + ", Offset: " + record.offset() + ", Value: " + record.value()); } } } } ``` 在上面的代码中,我们创建了一个KafkaConsumer实例,并使用assign()方法指定要消费的topic和分区,然后使用seekToBeginning()方法将消费者定位到该topic的开头。接着,我们使用poll()方法从Kafka获取一批记录,并遍历这些记录以获取每个分区的当前offset

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值