在Kafka消息中添加自定义标题 [英] Adding Custom Headers in Kafka Message

查看:30
本文介绍了在Kafka消息中添加自定义标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过使用 kafka 生产者将文件转换为字节数组来将文件作为消息发送.

I am sending a file as a message by converting it to a byte array using kafka producer.

我还需要为消息添加一些标头,例如文件名、时间戳等,以便在消费者端我可以根据文件名和其他标头处理消息.

I also need to add some headers for the message, for example file name, timestamps etc so at the consumer end I can process the message based on file name and other headers.

我目前正在做的是创建一个对象并将原始消息和标头包装在其中,并将该对象作为消息发送到字节数组中.

What I am currently doing is creating a object and wrapping the raw message and headers in it and sending the object in a byte array as a message.

我想知道是否有办法在发布消息时添加自定义标头?

I would like to know if there is a way by which I can add custom headers while publishing the message?

推荐答案

Kafka 不知道消息内容 并且没有提供任何特殊的方法来丰富它,所以这是你需要自己做的事情.处理这些事情的一种常见方法是使用结构化格式,例如 json、avro 或类似格式,您可以在其中自由定义必要的字段,并且可以轻松地将元数据添加到您的消息中并将其发送给 Kafka 代理.

Kafka is agnostic to the message content and doesn't provide any special means to enrich it so this is something you need to do yourself. A common way of dealing with these things is to use a structured format such as json, avro or similar where you are free to define the necessary fields and could easily add metadata to your message and ship it off to the Kafka brokers.

这个答案自 Kafka 0.11 起已过时,请参阅其他答案.

This answer is outdated as of Kafka 0.11, please see other answers.

这篇关于在Kafka消息中添加自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆