title: How to have Java kafka consumer skip messages?
body: <p>I currently have some code like this in my Kafka consumer:</p>
  
  <pre><code>KafkaStream&lt;byte[], byte[]&gt; stream = consumerMap.get(topic).get(0);
  ConsumerIterator&lt;byte[], byte[]&gt; it = stream.iterator();
  while(it.hasNext()){
      String receivedData = new String(it.next().message());
      // do some processing
  }
  </code></pre>
  
  <p>Which works really well, receiving every message. Now I want to skip over some messages, because e.g. I just want a sample of the messages. I don't want to have to receive them because I don't want to waste bandwidth. So e.g. I want something like below:</p>
  
  <pre><code>KafkaStream&lt;byte[], byte[]&gt; stream = consumerMap.get(topic).get(0);
  ConsumerIterator&lt;byte[], byte[]&gt; it = stream.iterator();
  while(it.hasNext()){
      if(sample){
          String receivedData = new String(it.next().message());
          // do some processing
      } else {
          it.skip(); // does not use bandwidth to receive message
      }
  }
  </code></pre>
  
  <p>In this case <code>it.skip()</code> should simply skip the message, not taking up bandwidth to actually transfer it over the network. I believe the answer has something to do with advancing offsets but I can't seem to find any specific functions that do this.</p>