How to Send Message to Specific Partition in Kafka
To send a message to a specific partition in Kafka, use the
KafkaProducer API and specify the partition number in the ProducerRecord constructor. You can also control partitioning by providing a key that Kafka hashes to select the partition.Syntax
The KafkaProducer.send() method allows specifying the partition explicitly by passing the partition number in the ProducerRecord constructor. The method signature looks like this:
send(ProducerRecord<K, V> record)- sends message with automatic partitioningsend(ProducerRecord<K, V> record, Callback callback)- sends message with callbackProducerRecord(String topic, Integer partition, K key, V value)- constructor wherepartitionis the target partition number
By creating a ProducerRecord with a specific partition number, you control exactly where the message goes.
java
ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", 2, "key1", "message to partition 2"); producer.send(record);
Example
This example shows how to send a message to partition 1 of topic my-topic using the Java Kafka client.
java
import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.common.serialization.StringSerializer; import java.util.Properties; public class SendToPartition { public static void main(String[] args) { Properties props = new Properties(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); KafkaProducer<String, String> producer = new KafkaProducer<>(props); // Create record for partition 1 ProducerRecord<String, String> record = new ProducerRecord<>("my-topic", 1, "myKey", "Hello Partition 1"); producer.send(record, (metadata, exception) -> { if (exception == null) { System.out.println("Sent to partition: " + metadata.partition() + ", offset: " + metadata.offset()); } else { exception.printStackTrace(); } }); producer.flush(); producer.close(); } }
Output
Sent to partition: 1, offset: 0
Common Pitfalls
- Not specifying partition: If you omit the partition number, Kafka uses the key or round-robin to assign partitions automatically.
- Invalid partition number: Specifying a partition number that does not exist for the topic causes an error.
- Ignoring key: If you want consistent partitioning by key, provide a key instead of hardcoding partition.
Example of wrong and right ways:
java
// Wrong: No partition specified, message goes to any partition ProducerRecord<String, String> record1 = new ProducerRecord<>("my-topic", "key1", "message"); // Right: Specify partition explicitly ProducerRecord<String, String> record2 = new ProducerRecord<>("my-topic", 0, "key1", "message to partition 0");
Quick Reference
| Concept | Usage |
|---|---|
| Specify partition | Use ProducerRecord(topic, partition, key, value) |
| Send message | producer.send(record) |
| Partition by key | Use ProducerRecord(topic, key, value) without partition |
| Check partition count | Use AdminClient or Kafka tools to verify topic partitions |
Key Takeaways
Use ProducerRecord constructor with partition number to send to a specific partition.
Providing a key lets Kafka decide partition consistently via hashing.
Always verify the partition number exists for the topic to avoid errors.
Omitting partition sends messages using Kafka's default partitioner.
Use callbacks to confirm message delivery and partition assignment.