I have a topic first_topic
with 3 partitions.
Here is my code, I send 55 messages to a consumer ( which is running in cmd ), the code below shows the partition to which the message was sent. Every time I launch the code all the messages go to one partition only ( that is picked randomly ), it may be partition 0, 1 or 2. Why doesn't round-robin work here? I do not specify the key, so I hope it should.
Logger logger = LoggerFactory.getLogger(Producer.class);
Properties properties = new Properties();
properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
KafkaProducer<String, String> producer = new KafkaProducer(properties);
for (int i = 0; i < 55; i++) {
producer.send(new ProducerRecord<>("first_topic", "Hello world partitionCheck " + i), (recordMetadata, e) -> {
// executes every time a record is sent or Exception is thrown
if (e == null) {
// record was successfully sent
logger.info("metaData: " +
"topic " + recordMetadata.topic() +
" Offset " + recordMetadata.offset() +
" TimeStamp " + recordMetadata.timestamp() +
" Partition " + recordMetadata.partition());
} else {
logger.error(e.toString());
}
});
}
producer.flush();
producer.close();