集群同步-》同步消费位点
This commit is contained in:
@@ -173,6 +173,18 @@ class ConsumerConsole(config: KafkaConfig) extends KafkaConsole(config: KafkaCon
|
||||
}).asInstanceOf[(Boolean, String)]
|
||||
}
|
||||
|
||||
def listSubscribeTopics(groupId: String): util.Map[String, util.List[TopicPartition]] = {
|
||||
val commitOffs = getCommittedOffsets(groupId)
|
||||
val map: util.Map[String, util.List[TopicPartition]] = new util.HashMap[String, util.List[TopicPartition]]()
|
||||
for (t <- commitOffs.keySet) {
|
||||
if (!map.containsKey(t.topic())) {
|
||||
map.put(t.topic(), new util.ArrayList[TopicPartition]())
|
||||
}
|
||||
map.get(t.topic()).add(t)
|
||||
}
|
||||
map
|
||||
}
|
||||
|
||||
private def describeConsumerGroups(groupIds: util.Set[String]): mutable.Map[String, ConsumerGroupDescription] = {
|
||||
withAdminClientAndCatchError(admin => {
|
||||
admin.describeConsumerGroups(groupIds).describedGroups().asScala.map {
|
||||
|
||||
@@ -65,6 +65,10 @@ class KafkaConsole(config: KafkaConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
protected def createAdminClient(props: Properties): Admin = {
|
||||
Admin.create(props)
|
||||
}
|
||||
|
||||
private def createAdminClient(): Admin = {
|
||||
Admin.create(getProps())
|
||||
}
|
||||
|
||||
39
src/main/scala/kafka/console/OperationConsole.scala
Normal file
39
src/main/scala/kafka/console/OperationConsole.scala
Normal file
@@ -0,0 +1,39 @@
|
||||
package kafka.console
|
||||
|
||||
import java.util.Properties
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
import com.xuxd.kafka.console.config.KafkaConfig
|
||||
|
||||
import scala.jdk.CollectionConverters.{ListHasAsScala, MapHasAsScala}
|
||||
|
||||
/**
|
||||
* kafka-console-ui.
|
||||
*
|
||||
* @author xuxd
|
||||
* @date 2021-10-24 23:23:30
|
||||
* */
|
||||
class OperationConsole(config: KafkaConfig, topicConsole: TopicConsole,
|
||||
consumerConsole: ConsumerConsole) extends KafkaConsole(config: KafkaConfig) with Logging {
|
||||
|
||||
def syncConsumerOffset(groupId: String, topic: String, props: Properties): (Boolean, String) = {
|
||||
val thatAdmin = createAdminClient(props)
|
||||
try {
|
||||
val thisTopicPartitions = consumerConsole.listSubscribeTopics(groupId).get(topic).asScala.sortBy(_.partition())
|
||||
|
||||
val thatTopicPartitions = thatAdmin.listConsumerGroupOffsets(
|
||||
groupId
|
||||
).partitionsToOffsetAndMetadata.get(timeoutMs, TimeUnit.MILLISECONDS).asScala.filter(_._1.topic().equals(topic)).keySet.toList.sortBy(_.partition())
|
||||
if (thatTopicPartitions != thisTopicPartitions) {
|
||||
throw new IllegalStateException("topic partition inconsistent.")
|
||||
}
|
||||
|
||||
} catch {
|
||||
case ex => throw ex
|
||||
} finally {
|
||||
thatAdmin.close()
|
||||
}
|
||||
|
||||
(true, "")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user