I am introducing with kafka and I want to know how to specify partition when I consume messages from topic.
I have found several picture like this:
It means that 1 consumer can consume messages from several partitions but 1 partition can be read by single consumer(within consumer group)
Also I have read several examples for consumer and it looks 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:
1.subscribe:
consumer.subscribe(Arrays.asList(“foo”, “bar”));
2. 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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…