Keys become useful when a user wants to introduce ordering and ensure the messages that share the same key end up in the same partition.
The ProducerRecord constructor accepts a key as shown below:
https://www.conduktor.io/kafka/complete-kafka-producer-with-java/