title: How to get all the messages from kafka topic and count them using java?
body: <p>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</p>
  
  <pre><code>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&lt;String,Integer&gt; topicCountMap = 
              new HashMap&lt;String,Integer&gt;();
      topicCountMap.put(topic, 1);
      Map&lt;String,List&lt;KafkaStream&lt;byte[],byte[]&gt;&gt;&gt; 
      consumerMap = consumer.createMessageStreams(topicCountMap);
      KafkaStream&lt;byte[],byte[]&gt; stream = consumerMap.get(topic).get(0);
      ConsumerIterator&lt;byte[],byte[]&gt; it =  stream.iterator();
      List&lt;String&gt; msgTopicList = new ArrayList&lt;String&gt;();
      int count = 0;
      System.out.println("Waiting");
      while(it.hasNext()){
          MessageAndMetadata&lt;byte[],byte[]&gt; msgAndData = it.next(); 
          String msg = new String(msgAndData.message());
          msgTopicList.add(msg);
          String key = "NoKey";
          System.out.println(msg);
          count++;
      }
  }
  }
  </code></pre>
  
  <p>What I have to do is get all the messages from the topic sent them to the user and count them</p>
  
  <p>What is the best way to do this?</p>
  
  <p>version  kafka_2.10-0.8.1.2.2.4.2-2</p>