use of io.confluent.ksql.services.KafkaConsumerGroupClient in project ksql by confluentinc.
the class ListTopicsExecutor method execute.
public static StatementExecutorResponse execute(final ConfiguredStatement<ListTopics> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
final KafkaTopicClient client = serviceContext.getTopicClient();
final Map<String, TopicDescription> topicDescriptions = listTopics(client, statement);
if (statement.getStatement().getShowExtended()) {
final KafkaConsumerGroupClient consumerGroupClient = new KafkaConsumerGroupClientImpl(serviceContext::getAdminClient);
final Map<String, List<Integer>> topicConsumersAndGroupCount = getTopicConsumerAndGroupCounts(consumerGroupClient);
final List<KafkaTopicInfoExtended> topicInfoExtendedList = topicDescriptions.values().stream().map(desc -> topicDescriptionToTopicInfoExtended(desc, topicConsumersAndGroupCount)).collect(Collectors.toList());
return StatementExecutorResponse.handled(Optional.of(new KafkaTopicsListExtended(statement.getStatementText(), topicInfoExtendedList)));
} else {
final List<KafkaTopicInfo> topicInfoList = topicDescriptions.values().stream().map(ListTopicsExecutor::topicDescriptionToTopicInfo).collect(Collectors.toList());
return StatementExecutorResponse.handled(Optional.of(new KafkaTopicsList(statement.getStatementText(), topicInfoList)));
}
}
use of io.confluent.ksql.services.KafkaConsumerGroupClient in project ksql by confluentinc.
the class ListTopicsExecutor method getTopicConsumerAndGroupCounts.
/**
* @return all topics with their associated consumerCount and consumerGroupCount
*/
private static Map<String, List<Integer>> getTopicConsumerAndGroupCounts(final KafkaConsumerGroupClient consumerGroupClient) {
final List<String> consumerGroups = consumerGroupClient.listGroups();
final Map<String, AtomicInteger> topicConsumerCount = new HashMap<>();
final Map<String, Set<String>> topicConsumerGroupCount = new HashMap<>();
for (final String group : consumerGroups) {
final Collection<ConsumerSummary> consumerSummaryList = consumerGroupClient.describeConsumerGroup(group).consumers();
for (final KafkaConsumerGroupClientImpl.ConsumerSummary summary : consumerSummaryList) {
for (final TopicPartition topicPartition : summary.partitions()) {
topicConsumerCount.computeIfAbsent(topicPartition.topic(), k -> new AtomicInteger()).incrementAndGet();
topicConsumerGroupCount.computeIfAbsent(topicPartition.topic(), k -> new HashSet<>()).add(group);
}
}
}
final HashMap<String, List<Integer>> results = new HashMap<>();
topicConsumerCount.forEach((k, v) -> {
results.computeIfAbsent(k, v1 -> new ArrayList<>()).add(v.intValue());
results.get(k).add(topicConsumerGroupCount.get(k).size());
});
return results;
}
Aggregations