Search in sources :

Example 6 with KafkaStream

use of kafka.consumer.KafkaStream in project voltdb by VoltDB.

the class KafkaLoader method getConsumerExecutor.

private ExecutorService getConsumerExecutor(KafkaConsumerConnector consumer, CSVDataLoader loader) throws Exception {
    Map<String, Integer> topicCountMap = new HashMap<>();
    // generate as many threads as there are partitions defined in kafka config
    ExecutorService executor = Executors.newFixedThreadPool(m_config.kpartitions);
    topicCountMap.put(m_config.topic, m_config.kpartitions);
    Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.m_consumer.createMessageStreams(topicCountMap);
    List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(m_config.topic);
    // now launch all the threads for partitions.
    for (final KafkaStream stream : streams) {
        KafkaConsumer bconsumer = new KafkaConsumer(stream, loader, m_config);
        executor.submit(bconsumer);
    }
    return executor;
}
Also used : HashMap(java.util.HashMap) ExecutorService(java.util.concurrent.ExecutorService) List(java.util.List) KafkaStream(kafka.consumer.KafkaStream)

Example 7 with KafkaStream

use of kafka.consumer.KafkaStream in project incubator-atlas by apache.

the class KafkaNotification method createConsumers.

@VisibleForTesting
public <T> List<NotificationConsumer<T>> createConsumers(NotificationType notificationType, int numConsumers, boolean autoCommitEnabled) {
    String topic = TOPIC_MAP.get(notificationType);
    Properties consumerProperties = getConsumerProperties(notificationType);
    List<NotificationConsumer<T>> consumers = new ArrayList<>(numConsumers);
    for (int i = 0; i < numConsumers; i++) {
        ConsumerConnector consumerConnector = createConsumerConnector(consumerProperties);
        Map<String, Integer> topicCountMap = new HashMap<>();
        topicCountMap.put(topic, 1);
        StringDecoder decoder = new StringDecoder(null);
        Map<String, List<KafkaStream<String, String>>> streamsMap = consumerConnector.createMessageStreams(topicCountMap, decoder, decoder);
        List<KafkaStream<String, String>> kafkaConsumers = streamsMap.get(topic);
        for (KafkaStream stream : kafkaConsumers) {
            KafkaConsumer<T> kafkaConsumer = createKafkaConsumer(notificationType.getClassType(), notificationType.getDeserializer(), stream, i, consumerConnector, autoCommitEnabled);
            consumers.add(kafkaConsumer);
        }
        consumerConnectors.add(consumerConnector);
    }
    return consumers;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringDecoder(kafka.serializer.StringDecoder) ConsumerConnector(kafka.javaapi.consumer.ConsumerConnector) KafkaStream(kafka.consumer.KafkaStream) Properties(java.util.Properties) ApplicationProperties(org.apache.atlas.ApplicationProperties) NotificationConsumer(org.apache.atlas.notification.NotificationConsumer) ArrayList(java.util.ArrayList) List(java.util.List) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with KafkaStream

use of kafka.consumer.KafkaStream in project incubator-atlas by apache.

the class KafkaNotificationMockTest method testCreateConsumers.

@Test
@SuppressWarnings("unchecked")
public void testCreateConsumers() throws Exception {
    Properties properties = mock(Properties.class);
    when(properties.getProperty("entities.group.id")).thenReturn("atlas");
    final ConsumerConnector consumerConnector = mock(ConsumerConnector.class);
    Map<String, Integer> topicCountMap = new HashMap<>();
    topicCountMap.put(KafkaNotification.ATLAS_ENTITIES_TOPIC, 1);
    Map<String, List<KafkaStream<String, String>>> kafkaStreamsMap = new HashMap<>();
    List<KafkaStream<String, String>> kafkaStreams = new ArrayList<>();
    KafkaStream kafkaStream = mock(KafkaStream.class);
    kafkaStreams.add(kafkaStream);
    kafkaStreamsMap.put(KafkaNotification.ATLAS_ENTITIES_TOPIC, kafkaStreams);
    when(consumerConnector.createMessageStreams(eq(topicCountMap), any(StringDecoder.class), any(StringDecoder.class))).thenReturn(kafkaStreamsMap);
    final KafkaConsumer consumer1 = mock(KafkaConsumer.class);
    final KafkaConsumer consumer2 = mock(KafkaConsumer.class);
    KafkaNotification kafkaNotification = new TestKafkaNotification(properties, consumerConnector, consumer1, consumer2);
    List<NotificationConsumer<String>> consumers = kafkaNotification.createConsumers(NotificationInterface.NotificationType.ENTITIES, 2);
    verify(consumerConnector, times(2)).createMessageStreams(eq(topicCountMap), any(StringDecoder.class), any(StringDecoder.class));
    assertEquals(consumers.size(), 2);
    assertTrue(consumers.contains(consumer1));
    assertTrue(consumers.contains(consumer2));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) StringDecoder(kafka.serializer.StringDecoder) ConsumerConnector(kafka.javaapi.consumer.ConsumerConnector) KafkaStream(kafka.consumer.KafkaStream) Properties(java.util.Properties) NotificationConsumer(org.apache.atlas.notification.NotificationConsumer) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.testng.annotations.Test)

Aggregations

KafkaStream (kafka.consumer.KafkaStream)8 List (java.util.List)7 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 ConsumerConnector (kafka.javaapi.consumer.ConsumerConnector)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 ExecutorService (java.util.concurrent.ExecutorService)3 MessageAndMetadata (kafka.message.MessageAndMetadata)3 ConsumerConfig (kafka.consumer.ConsumerConfig)2 Whitelist (kafka.consumer.Whitelist)2 StringDecoder (kafka.serializer.StringDecoder)2 NotificationConsumer (org.apache.atlas.notification.NotificationConsumer)2 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Stopwatch (com.google.common.base.Stopwatch)1 ByteBufferInputRowParser (io.druid.data.input.ByteBufferInputRowParser)1 Firehose (io.druid.data.input.Firehose)1 IAE (io.druid.java.util.common.IAE)1 ISE (io.druid.java.util.common.ISE)1