Quantcast
Channel: Active questions tagged consumer - Stack Overflow
Viewing all articles
Browse latest Browse all 91

How to get all the messages from kafka topic and count them using java?

$
0
0

This code sometimes gives me all the messages from the beginning and waiting for another message and sometimes it's just waiting for another message

import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Properties;import kafka.consumer.ConsumerConfig;import kafka.consumer.ConsumerIterator;import kafka.consumer.KafkaStream;import kafka.javaapi.consumer.ConsumerConnector;import kafka.message.MessageAndMetadata;public class TestConsumer{public static void main(String[] args) {    ConsumerConfig config;    Properties props = new Properties();     props.put("zookeeper.connect","sandbox.hortonworks.com:2181");    props.put("group.id", "group-4");    props.put("zookeeper.session.timeout.ms", "400");    props.put("zookeeper.sync.time.ms", "200");    props.put("auto.commit.interval.ms", "200");    config = new ConsumerConfig(props);    ConsumerConnector consumer = kafka.consumer.Consumer.createJavaConsumerConnector            (config);    String topic = "News";     System.out.println("Running");    Run(consumer,topic); }public static void Run(ConsumerConnector consumer,String topic){    HashMap<String,Integer> topicCountMap =             new HashMap<String,Integer>();    topicCountMap.put(topic, 1);    Map<String,List<KafkaStream<byte[],byte[]>>>     consumerMap = consumer.createMessageStreams(topicCountMap);    KafkaStream<byte[],byte[]> stream = consumerMap.get(topic).get(0);    ConsumerIterator<byte[],byte[]> it =  stream.iterator();    List<String> msgTopicList = new ArrayList<String>();    int count = 0;    System.out.println("Waiting");    while(it.hasNext()){        MessageAndMetadata<byte[],byte[]> msgAndData = it.next();         String msg = new String(msgAndData.message());        msgTopicList.add(msg);        String key = "NoKey";        System.out.println(msg);        count++;    }}}

What I have to do is get all the messages from the topic sent them to the user and count them

What is the best way to do this?

version kafka_2.10-0.8.1.2.2.4.2-2


Viewing all articles
Browse latest Browse all 91

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>