[英]Kafka how to know the latest created partition id for a topic in Java
I am creating partition for a topic dynamically using AdminUtils. 我正在使用AdminUtils动态创建主题分区。 I have to get the partitionId of the newly created partition for saving it in Mysql for my business logic.
我必须获取新创建的分区的partitionId才能将其保存在Mysql中以用于业务逻辑。 How can I achieve this ?
我该如何实现? My current code :
我当前的代码:
AdminUtils.addPartitions(zkUtils,KAFKA_PRODUCER_TOPIC,currentPartitionCount+1,"",true);
List<PartitionInfo> infoList = producer.partitionsFor(KAFKA_PRODUCER_TOPIC);
for(PartitionInfo info : infoList){
System.out.println(info.partition());
}
createdPartionId = infoList.get(0).partition();
Problem with above is that latest created partition is not coming in the PartitionInfo list. 上面的问题是,最新创建的分区没有出现在PartitionInfo列表中。 Don't know why.producer is the Kafka Producer API.
不知道为什么。producer是Kafka Producer API。
You can do a TopicMetadataRequest
. 您可以执行
TopicMetadataRequest
。 See the example . 参见示例 。 You can do one before and after and compare the results.
您可以在前后进行比较,然后比较结果。 Otherwise, the new partition always gets added with a
newPartitionID == previousLastPartitionID + 1
否则,新分区将始终添加有
newPartitionID == previousLastPartitionID + 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.