Java Kafka 对象序列化器和反序列化器 [英] Java Kafka Object serilizer and deserializer
问题描述
我在做 kafka 生产者和消费者.序列化和反序列化三种方式.
I am doing kafka producers and consumers. there are three ways to do Serialization and de-serialization.
1、自定义对象 -> byte[] -> 对象 (示例)
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[]:在这种情况下,发生了三件事.
- Object -> byte[]: In this case three things are happening.
- 对象被转换为 Json 对象树.
- 树被转换为字符串.
- 字符串被转换为字节.
- 我假设您使用的是对象映射器,它在内部构建了一个 Json 对象树并将树转换为字符串.
- 我还假设您正在使用 StringSerializer,它让 kafka 将字符串转换为字节.
- 生成 Json 对象树的对象映射器.
- JsonSerializer 将树转换为字符串,将字符串转换为字节.
如果您对最大化性能感兴趣,您可能希望避免使用 json 作为序列化机制并探索 protobuf.一个 kafka protobuf 示例是这里.此处 比较了 protobuf 性能与 json 序列化的一些数字.
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屋!