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

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

问题描述

我在做 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.

  1. Object -> byte[]:在这种情况下,发生了三件事.
  1. Object -> byte[]: In this case three things are happening.
  1. 对象被转换为 Json 对象树.
  2. 树被转换为字符串.
  3. 字符串被转换为字节.

  • Object -> String:在这种情况下,发生了同样的三件事.

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

    1. 我假设您使用的是对象映射器,它在内部构建了一个 Json 对象树并将树转换为字符串.
    2. 我还假设您正在使用 StringSerializer,它让 kafka 将字符串转换为字节.

  • Object -> JsonNode:同样的三件事正在发生.

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

    1. 生成 Json 对象树的对象映射器.
    2. 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屋!

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