public static KafkaTopicsList build( final String statementText, final Collection<KsqlTopic> ksqlTopics, final Map<String, TopicDescription> kafkaTopicDescriptions, final KsqlConfig ksqlConfig, final KafkaConsumerGroupClient consumerGroupClient ) { final Set<String> registeredNames = getRegisteredKafkaTopicNames(ksqlTopics); final List<KafkaTopicInfo> kafkaTopicInfoList = new ArrayList<>(); final Map<String, TopicDescription> filteredDescriptions = new TreeMap<>( filterKsqlInternalTopics(kafkaTopicDescriptions, ksqlConfig)); final Map<String, List<Integer>> topicConsumersAndGroupCount = getTopicConsumerAndGroupCounts( consumerGroupClient); for (final TopicDescription desp : filteredDescriptions.values()) { kafkaTopicInfoList.add(new KafkaTopicInfo( desp.name(), registeredNames.contains(desp.name()), desp.partitions() .stream().map(partition -> partition.replicas().size()).collect(Collectors.toList()), topicConsumersAndGroupCount.getOrDefault(desp.name(), Arrays.asList(0, 0)).get(0), topicConsumersAndGroupCount.getOrDefault(desp.name(), Arrays.asList(0, 0)).get(1) )); } return new KafkaTopicsList(statementText, kafkaTopicInfoList); }
@Override public Table buildTable(final KafkaTopicsList entity) { final Stream<List<String>> rows = entity.getTopics().stream() .map(t -> ImmutableList.of( t.getName(), Boolean.toString(t.getRegistered()), Integer.toString(t.getReplicaInfo().size()), getTopicReplicaInfo(t.getReplicaInfo()), Integer.toString(t.getConsumerCount()), Integer.toString(t.getConsumerGroupCount()))); return new Builder() .withColumnHeaders(HEADERS) .withRows(rows) .build(); }
@Test public void shouldBuildValidTopicList() { final Collection<KsqlTopic> ksqlTopics = Collections.emptyList(); // represent the full list of topics final Map<String, TopicDescription> topicDescriptions = new HashMap<>(); final TopicPartitionInfo topicPartitionInfo = new TopicPartitionInfo(1, new Node(1, "", 8088), Collections.emptyList(), Collections.emptyList()); topicDescriptions.put("test-topic", new TopicDescription("test-topic", false, Collections.singletonList(topicPartitionInfo))); /** * Return POJO for consumerGroupClient */ final TopicPartition topicPartition = new TopicPartition("test-topic", 1); final KafkaConsumerGroupClientImpl.ConsumerSummary consumerSummary = new KafkaConsumerGroupClientImpl.ConsumerSummary("consumer-id"); consumerSummary.addPartition(topicPartition); final KafkaConsumerGroupClientImpl.ConsumerGroupSummary consumerGroupSummary = new KafkaConsumerGroupClientImpl.ConsumerGroupSummary(Collections.singleton(consumerSummary)); final KafkaConsumerGroupClient consumerGroupClient = mock(KafkaConsumerGroupClient.class); expect(consumerGroupClient.listGroups()).andReturn(Collections.singletonList("test-topic")); expect(consumerGroupClient.describeConsumerGroup("test-topic")).andReturn(consumerGroupSummary); replay(consumerGroupClient); /** * Test */ final KafkaTopicsList topicsList = KafkaTopicsList.build("statement test", ksqlTopics, topicDescriptions, new KsqlConfig(Collections.EMPTY_MAP), consumerGroupClient); assertThat(topicsList.getTopics().size(), equalTo(1)); final KafkaTopicInfo first = topicsList.getTopics().iterator().next(); assertThat(first.getConsumerGroupCount(), equalTo(1)); assertThat(first.getConsumerCount(), equalTo(1)); assertThat(first.getReplicaInfo().size(), equalTo(1)); }
@Test public void testSerde() throws Exception { final ObjectMapper mapper = JsonMapper.INSTANCE.mapper; final KafkaTopicsList expected = new KafkaTopicsList( "SHOW TOPICS;", ImmutableList.of(new KafkaTopicInfo("thetopic", true, ImmutableList.of(1, 2, 3), 42, 12)) ); final String json = mapper.writeValueAsString(expected); assertEquals( "{\"@type\":\"kafka_topics\",\"statementText\":\"SHOW TOPICS;\"," + "\"topics\":[{\"name\":\"thetopic\",\"registered\":true," + "\"replicaInfo\":[1,2,3],\"consumerCount\":42," + "\"consumerGroupCount\":12}]}", json); final KafkaTopicsList actual = mapper.readValue(json, KafkaTopicsList.class); assertEquals(expected, actual); } }
new TablesList("e", ImmutableList.of(new SourceInfo.Table("TestTable", "TestTopic", "JSON", false))), new KsqlTopicsList("e", ImmutableList.of(new KsqlTopicInfo("TestTopic", "TestKafkaTopic", DataSource.DataSourceSerDe.JSON))), new KafkaTopicsList("e", ImmutableList.of(new KafkaTopicInfo("TestKafkaTopic", true, ImmutableList.of(1), 1, 1))), new ExecutionPlan("Test Execution Plan") ));