Search in sources :

Example 1 with KafkaTopic

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;
}
Also used : KafkaTopic(org.apache.metron.rest.model.KafkaTopic) PartitionInfo(org.apache.kafka.common.PartitionInfo)

Example 2 with 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());
}
Also used : HashMap(java.util.HashMap) Node(org.apache.kafka.common.Node) KafkaTopic(org.apache.metron.rest.model.KafkaTopic) ArrayList(java.util.ArrayList) List(java.util.List) PartitionInfo(org.apache.kafka.common.PartitionInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

PartitionInfo (org.apache.kafka.common.PartitionInfo)2 KafkaTopic (org.apache.metron.rest.model.KafkaTopic)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Node (org.apache.kafka.common.Node)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1