Search in sources :

Example 6 with MessageAndMetadata

use of kafka.message.MessageAndMetadata in project ignite by apache.

the class KafkaIgniteStreamerSelfTest method consumerStream.

/**
 * Consumes Kafka stream via Ignite.
 *
 * @param topic Topic name.
 * @param keyValMap Expected key value map.
 * @throws TimeoutException If timed out.
 * @throws InterruptedException If interrupted.
 */
private void consumerStream(String topic, Map<String, String> keyValMap) throws TimeoutException, InterruptedException {
    KafkaStreamer<String, String> kafkaStmr = null;
    Ignite ignite = grid();
    try (IgniteDataStreamer<String, String> stmr = ignite.dataStreamer(DEFAULT_CACHE_NAME)) {
        stmr.allowOverwrite(true);
        stmr.autoFlushFrequency(10);
        // Configure Kafka streamer.
        kafkaStmr = new KafkaStreamer<>();
        // Get the cache.
        IgniteCache<String, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
        // Set Ignite instance.
        kafkaStmr.setIgnite(ignite);
        // Set data streamer instance.
        kafkaStmr.setStreamer(stmr);
        // Set the topic.
        kafkaStmr.setTopic(topic);
        // Set the number of threads.
        kafkaStmr.setThreads(4);
        // Set the consumer configuration.
        kafkaStmr.setConsumerConfig(createDefaultConsumerConfig(embeddedBroker.getZookeeperAddress(), "groupX"));
        kafkaStmr.setMultipleTupleExtractor(new StreamMultipleTupleExtractor<MessageAndMetadata<byte[], byte[]>, String, String>() {

            @Override
            public Map<String, String> extract(MessageAndMetadata<byte[], byte[]> msg) {
                Map<String, String> entries = new HashMap<>();
                try {
                    String key = new String(msg.key());
                    String val = new String(msg.message());
                    // Convert the message into number of cache entries with same key or dynamic key from actual message.
                    // For now using key as cache entry key and value as cache entry value - for test purpose.
                    entries.put(key, val);
                } catch (Exception ex) {
                    fail("Unexpected error." + ex);
                }
                return entries;
            }
        });
        // Start kafka streamer.
        kafkaStmr.start();
        final CountDownLatch latch = new CountDownLatch(CNT);
        IgniteBiPredicate<UUID, CacheEvent> locLsnr = new IgniteBiPredicate<UUID, CacheEvent>() {

            @Override
            public boolean apply(UUID uuid, CacheEvent evt) {
                latch.countDown();
                return true;
            }
        };
        ignite.events(ignite.cluster().forCacheNodes(DEFAULT_CACHE_NAME)).remoteListen(locLsnr, null, EVT_CACHE_OBJECT_PUT);
        // Checks all events successfully processed in 10 seconds.
        assertTrue(latch.await(10, TimeUnit.SECONDS));
        for (Map.Entry<String, String> entry : keyValMap.entrySet()) assertEquals(entry.getValue(), cache.get(entry.getKey()));
    } finally {
        if (kafkaStmr != null)
            kafkaStmr.stop();
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) MessageAndMetadata(kafka.message.MessageAndMetadata) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) CacheEvent(org.apache.ignite.events.CacheEvent) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

MessageAndMetadata (kafka.message.MessageAndMetadata)6 List (java.util.List)3 Properties (java.util.Properties)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 KafkaStream (kafka.consumer.KafkaStream)3 HashMap (java.util.HashMap)2 TimeoutException (java.util.concurrent.TimeoutException)2 ConsumerConfig (kafka.consumer.ConsumerConfig)2 ConsumerIterator (kafka.consumer.ConsumerIterator)2 Whitelist (kafka.consumer.Whitelist)2 InstrumentedExecutorService (com.codahale.metrics.InstrumentedExecutorService)1 Stopwatch (com.google.common.base.Stopwatch)1 IAE (io.druid.java.util.common.IAE)1 ISE (io.druid.java.util.common.ISE)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 UUID (java.util.UUID)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ExecutorService (java.util.concurrent.ExecutorService)1