I am learning Kafka and I want to know how to specify the partition when I consume messages from a topic.
I have found several pictures like this:
Image may be NSFW.
Clik here to view.
It means that a consumer can consume messages from several partitions but a partition can only be read by a single consumer (within a consumer group).
Also, I have read several examples for consumer and they look like this:
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<String, String> consumer = new KafkaConsumer<>(props);
and:
Subscribe:
consumer.subscribe(Arrays.asList(“foo”, “bar”));
Poll
try { while (running) { ConsumerRecords<String, String> records = consumer.poll(1000); for (ConsumerRecord<String, String> record : records) System.out.println(record.offset() +": " + record.value()); } } finally { consumer.close(); }
How does this work? From which partition will I read messages?