title: How to consume all messages from begining from apache kafka using java
body: <p>I am trying to consume all messages from the beginning of a topic in Apache Kafka. I can consume messages which are producing at that moment. Here is my code for fetch messages.</p>
  
  <pre><code> public void consume() {
      KafkaConsumer&lt;String, String&gt; consumer = new KafkaConsumer&lt;&gt;(props);
      consumer.subscribe(Arrays.asList(topic));
      while (true) {
          ConsumerRecords&lt;String, String&gt; records = consumer.poll(1000);
          for (ConsumerRecord&lt;String, String&gt; record : records) {
              System.out.printf("\"%s\"\n", record.value());
          }
      }
  }
  </code></pre>