Java Kafka对象序列化器和反序列化器 [英] Java Kafka Object serilizer and deserializer
问题描述
我在做卡夫卡的生产者和消费者。有三种方法可以进行序列化和反序列化。
I am doing kafka producers and consumers. there are three ways to do Serialization and de-serialization.
1,自定义对象->字节[]->对象(示例)
1, custom object -> byte[] -> object (example)
2,自定义对象->字符串->对象(我尝试过工作)
2, custom object -> String -> object (I tried. working)
3,自定义对象-> JsonNode->对象(示例)
3, custom object -> JsonNode -> object (example)
哪个更好?谢谢
推荐答案
您可以尝试进行基准测试,但我想这三者大致相同。
You can try benchmarking but I would imagine all three are about the same.
- 对象->字节[] :在这种情况下,发生了三件事。
- Object -> byte[]: In this case three things are happening.
- 对象被转换为Json对象的树。
- 树被转换为字符串。
- 字符串转换为字节。
- 我假设您正在使用对象映射器,该对象映射器在内部构建Json对象树并将其转换为字符串。
- 我还假设您正在使用StringSerializer,它可以让kafka将字符串转换为字节。
- 生成Json对象树的对象映射器。
- JsonSerializer将树转换为字符串,然后将字符串转换为字节。
如果您想最大化性能,则可能需要为避免将json用作序列化机制,请浏览 protobuf 。
一个kafka protobuf示例是此处。 此处。。
If you're interested in maximizing performance you might want to avoid using json as a serialization mechanism and explore protobuf. A kafka protobuf example is here. Some numbers comparing protobuf performance vs json serialization are here.
这篇关于Java Kafka对象序列化器和反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!