简体   繁体   English

在kafka java中生成所有消息到主题后如何发送元数据?

[英]How to send Meta data after producing all message to topic in kafka java?

I am producing student details to a Kafka topic by using the Java Kafka client libraries and what I want is once all the records are produced, I want to send the message count with some other meta data to the same Kafka topic.我正在使用 Java Kafka 客户端库为 Kafka 主题生成学生详细信息,我想要的是一旦生成了所有记录,我想将消息计数与其他一些元数据一起发送到同一个 Kafka 主题。

 List<Student> allStudent;

suppose the size of the allStudent list is 10假设 allStudent 列表的大小是 10

Then once all student records are produced to the topic then I want to produce another message like: "10 records successfully produced to topic".然后,一旦所有学生记录都生成到该主题,那么我想生成另一条消息,例如:“10 条记录成功生成到主题”。

"10 record successfully produced to topic" - This should be the last message to be produced. “10 条记录成功生成到主题” - 这应该是要生成的最后一条消息。

The Kafka producer send() method returns a Future<RecordMetadata> object. Kafka 生产者send()方法返回Future<RecordMetadata> object。 One option would be to call the get() method on this Future.一种选择是在这个 Future 上调用get()方法。 Once the get() method returns, you can be sure that the data has been written to Kafka.一旦get()方法返回,就可以确定数据已经写入 Kafka。 Also you should probably configure your producer for durability ( acks=all setting).此外,您可能应该为生产者配置耐用性( acks=all设置)。 Once your get method returns, you can send your confirmation message.一旦您的 get 方法返回,您就可以发送确认消息。

Still I would consider sending those confirmation messages a questionable practice.我仍然会考虑发送这些确认消息是一种有问题的做法。 What are they good for?它们有什么用?

I am not sure what is the real business case for this.我不确定真正的商业案例是什么。

But in KAFKA we call this behaviour as a Control Message, which is a kind of Meta Data about the list of records previously sent for reconciliation purpose later, especially for the batch processing.但是在 KAFKA 中,我们将这种行为称为控制消息,它是一种关于先前发送的记录列表的元数据,用于以后进行对账,尤其是批处理。 But the control message is completely driven by the producer ie producer should have a counter of messages sent Synchronously or Asynchronously and send a control message to another topic, let's call it as a control topic, where a consumer subscribed to perform the delta comparison between the Data Topic (Event Schema) and Control Topic (Control Message Schema).但是控制消息完全由生产者驱动,即生产者应该有一个同步或异步发送的消息计数器,并将控制消息发送到另一个主题,我们称之为控制主题,消费者订阅执行之间的增量比较数据主题(事件模式)和控制主题(控制消息模式)。

Along with this, if I want to have an extremely high confidence that broker leaders and replicas have the data produced, you should use acks=all with right values set for replication factor and in-sync replicas parameters.除此之外,如果我想对代理领导者和副本产生数据有极高的信心,您应该使用acks=all并为复制因子和同步副本参数设置正确的值。

In summary, it is up to the producer how you want to implement this as KAFKA don't have a specific implementation for this.总之,这取决于生产者您希望如何实现这一点,因为 KAFKA 对此没有具体的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM