consumer.How to specify partition to read? [kafka] : <p>I am introducing with kafka and I want to know how to specify partition when I consume messages from topic.</p>

<p>I have found several picture like this:</p>

<p><a href="https://i.stack.imgur.com/PeZlZ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/PeZlZ.png" alt="enter image description here"></a></p>

<p>It means that 1 consumer can consume messages from several partitions but 1 partition can be read by single consumer(within consumer group) </p>

<p>Also I have read several examples for consumer and it looks like this:</p>

<pre><code>Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("group.id", "consumer-tutorial");
props.put("key.deserializer", StringDeserializer.class.getName());
props.put("value.deserializer", StringDeserializer.class.getName());
KafkaConsumer&lt;String, String&gt; consumer = new KafkaConsumer&lt;&gt;(props); 
</code></pre>

<p>and:  </p>

<h2>1.subscribe:</h2>

<pre><code>consumer.subscribe(Arrays.asList(“foo”, “bar”)); 
</code></pre>

<h2>2. poll</h2>

<pre><code> try {
      while (running) {
        ConsumerRecords&lt;String, String&gt; records = consumer.poll(1000);
        for (ConsumerRecord&lt;String, String&gt; record : records)
          System.out.println(record.offset() + ": " + record.value());
      }
    } finally {
      consumer.close();
    }
</code></pre>

<p>How does this work? From which partition will I read messages?  </p>
