Sobes.tech
Junior — Middle

Kuidas valib Kafka tootja saatmiseks jaotise (partition)?

sobes.tech AI

Vastus AI-lt

Kafka producer selects a partition to send a message based on the message key and partitioning settings. If the message has a key, Kafka uses a hash function of that key to determine the specific partition, ensuring that all messages with the same key go to the same partition. If there is no key, the producer typically distributes messages in a round-robin manner to balance the load.

Example in Go using the sarama library:

producer, _ := sarama.NewSyncProducer(brokers, config)
msg := &sarama.ProducerMessage{
    Topic: "my_topic",
    Key:   sarama.StringEncoder("userID123"), // key for partitioning
    Value: sarama.StringEncoder("message payload"),
}
partition, offset, err := producer.SendMessage(msg)

Here, the key "userID123" will be used to select the partition, so that all messages with this key go to the same partition.