Java Kafka对象序列化器和反序列化器 [英] Java Kafka Object serilizer and deserializer

查看:245
本文介绍了Java Kafka对象序列化器和反序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做卡夫卡的生产者和消费者。有三种方法可以进行序列化和反序列化。

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.


  1. 对象->字节[] :在这种情况下,发生了三件事。

  1. Object -> byte[]: In this case three things are happening.

  1. 对象被转换为Json对象的树。

  2. 树被转换为字符串。

  3. 字符串转换为字节。


  • 对象->字符串:在此如果发生了同样的三件事。

  • Object -> String: In this case the same three things are happening.


    1. 我假设您正在使用对象映射器,该对象映射器在内部构建Json对象树并将其转换为字符串。

    2. 我还假设您正在使用StringSerializer,它可以让kafka将字符串转换为字节。


  • 对象-> JsonNode :同样发生了三件事。

  • Object -> JsonNode: Again same three things are happening.


    1. 生成Json对象树的对象映射器。

    2. 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屋!

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