Reputation: 575
I am new to Kafka. As per my understanding, Topics are furthers divided into partitions producer send message.
public class KafkaSender {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
String kafkaTopic = "java_in_use_topic";
public void send(String message) {
kafkaTemplate.send(kafkaTopic, message);
}
}
As per this code we are only providing topic name to Producer. How it will decide which partition it has to put and similarly at consumer side also we are only giving topic name from which partition it has to read. How it will decide?
Upvotes: 2
Views: 1781
Reputation: 41
There are three possibilities.
1. Partition is specified in record - use that partition and send record on it.
2. no partition is specified but a key is present - then kafka use the hash of the key
It can be used when we want to distribute the data based on a key. The following formula is used to determine the partition:
hashCode(key) % noOfPartitions
3.If no partition or key is present - choose a partition in a round-robin fashion
Writing custom Partitioner Apache Kafka provides an interface called Partitioner, where you can implement your logic for partitioning
https://kafka.apache.org/10/javadoc/org/apache/kafka/clients/producer/Partitioner.html
Upvotes: 4