use of org.apache.metron.rest.model.KafkaTopic in project metron by apache.
the class KafkaServiceImpl method getTopic.
@Override
public KafkaTopic getTopic(final String name) {
KafkaTopic kafkaTopic = null;
if (listTopics().contains(name)) {
try (Consumer<String, String> consumer = kafkaConsumerFactory.createConsumer()) {
final List<PartitionInfo> partitionInfos = consumer.partitionsFor(name);
if (partitionInfos.size() > 0) {
final PartitionInfo partitionInfo = partitionInfos.get(0);
kafkaTopic = new KafkaTopic();
kafkaTopic.setName(name);
kafkaTopic.setNumPartitions(partitionInfos.size());
kafkaTopic.setReplicationFactor(partitionInfo.replicas().length);
}
}
}
return kafkaTopic;
}
use of org.apache.metron.rest.model.KafkaTopic in project metron by apache.
the class KafkaServiceImplTest method getTopicShouldProperlyMapTopicToKafkaTopic.
@Test
public void getTopicShouldProperlyMapTopicToKafkaTopic() throws Exception {
final PartitionInfo partitionInfo = mock(PartitionInfo.class);
when(partitionInfo.replicas()).thenReturn(new Node[] { new Node(1, "host", 8080) });
final Map<String, List<PartitionInfo>> topics = new HashMap<>();
topics.put("t", Lists.newArrayList(partitionInfo));
topics.put("t1", Lists.newArrayList());
final KafkaTopic expected = new KafkaTopic();
expected.setName("t");
expected.setNumPartitions(1);
expected.setReplicationFactor(1);
when(kafkaConsumer.listTopics()).thenReturn(topics);
when(kafkaConsumer.partitionsFor("t")).thenReturn(Lists.newArrayList(partitionInfo));
KafkaTopic actual = kafkaService.getTopic("t");
assertEquals(expected, actual);
assertEquals(expected.hashCode(), actual.hashCode());
}
Aggregations