[英]Having problems with Kafka AdminClient trying to get the lag of a consumer
我一直试图使用AdminClient来获取消费者的滞后,但是adminClient.listGroupOffsets(“ foo”); 返回一个空指针NullPointerException。 这是我的代码:
public long getLag() {
AdminClient adminClient = AdminClient.createSimplePlaintext("localhost:9092");
scala.collection.immutable.Map<TopicPartition, Object> offsets = adminClient.listGroupOffsets("foo");
Option<Object> offset = offsets.get(new TopicPartition("test", 0));
TopicPartition topicPartition = new TopicPartition("test", 0);
return getLogEndOffset(topicPartition)- Long.parseLong(offset.get().toString());
}
private long getLogEndOffset(TopicPartition tp) {
KafkaConsumer consumer = createNewConsumer();
Collections.singletonList(tp);
consumer.assign(Collections.singletonList(tp));
consumer.seekToEnd(Collections.singletonList(tp));
return consumer.position(tp);
}
private KafkaConsumer createNewConsumer() {
Properties properties = new Properties();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "g1");
properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
properties.put(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "30000");
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringDeserializer");
return new KafkaConsumer(properties);
}
listGroupOffsets
api在2.12中引入。 可能的原因是版本与代理版本不匹配。
代理仅支持OffsetFetchRequest
v1,但您需要v2或更高版本才能请求所有主题分区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.