当你说你批量发送 100 条消息时,你的意思是,你想控制这个消息的数量,或者可以让生产者批量发送一定数量的消息,然后批量发送?
由于不确定您是否可以控制一个生产者批次中生成的消息数量,API 会将它们排队并为您批处理,但不能保证将它们全部一起批处理(不过我会检查一下)。
如果您可以让 API 为您批处理一定数量的消息,这里有一些关于如何确认消息的线索。
在与生产者打交道时,Kafka 在写入(也称为“批量写入”)方面具有某种可靠性
正如此幻灯片分享帖子中所述:
https://www.slideshare.net/miguno/apache-kafka-08-basic-training-verisign (83)
The original list of messages is partitioned (randomly if the default partitioner is used) based on their destination partitions/topics, i.e. split into smaller batches.
Each post-split batch is sent to the respective leader broker/ISR (the individual send()’s happen sequentially), and each is acked by its respective leader broker according to request.required.acks
所以关于原子性.. 对于上述行为,不确定整个批次是否会被视为原子性。也许您可以确保为每条消息使用相同的密钥发送您的一批消息,因为它们将进入同一个分区,因此可能成为原子
如果您在制作时需要更清楚地了解确认规则,请看这里是如何工作的 如这里所述https://docs.confluent.io/current/clients/producer.html:
You can control the durability of messages written to Kafka through the acks setting.
The default value of "1" requires an explicit acknowledgement from the partition leader that the write succeeded.
The strongest guarantee that Kafka provides is with "acks=all", which guarantees that not only did the partition leader accept the write, but it was successfully replicated to all of the in-sync replicas.
如果您的目标是在生产时没有重复,您还可以查看 producer enable.idempotence 行为。
亚尼克