[英]How to get a Kafka Topic Lag in Java
I want to see the lag position of a kafka topic in java. 我想在Java中查看kafka主题的滞后位置。 someone here says that below code will work.
这里有人说下面的代码可以工作。
AdminClient client = AdminClient.createSimplePlaintext("localhost:9092");
Map<TopicPartition, Object> offsets = JavaConversions.asJavaMap(
client.listGroupOffsets("groupID"));
Long offset = (Long) offsets.get(new TopicPartition("topic", 0));
But when I tried to import kafka.admin.AdminClient that listGroupOffsets method is not there. 但是当我尝试导入kafka.admin.AdminClient时 , listGroupOffsets方法不存在。 Please help me with this.
请帮我解决一下这个。
Method listGroupOffsets
was introduced to AdminClient.scala
starting 0.10.2. 从0.10.2开始,方法
listGroupOffsets
已引入AdminClient.scala
。 See KAFKA-3853 for details. 有关详细信息,请参见KAFKA-3853 。 So you should use Kafka 0.10.2.0 or upwards.
因此,您应该使用Kafka 0.10.2.0或更高版本。
您可以使用https://github.com/yahoo/kafka-manager并可以使用其http Rest API获取消费者组延迟和其他详细信息。
I am using Spring framework. 我正在使用Spring框架。 Using the below code, you can get the metrics via java.The code works.
使用下面的代码,您可以通过java获取指标。
@Component
public class Receiver {
private static final Logger LOGGER =
LoggerFactory.getLogger(Receiver.class);
@Autowired
private KafkaListenerEndpointRegistry kafkaListenerEndpointRegistry;
public void testlag() {
for (MessageListenerContainer messageListenerContainer : kafkaListenerEndpointRegistry
.getListenerContainers()) {
Map<String, Map<MetricName, ? extends Metric>> metrics = messageListenerContainer.metrics();
metrics.forEach( (clientid, metricMap) ->{
System.out.println("------------------------For client id : "+clientid);
metricMap.forEach((metricName,metricValue)->{
//if(metricName.name().contains("lag"))
System.out.println("------------Metric name: "+metricName.name()+"-----------Metric value: "+metricValue.metricValue());
});
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.