Java Kafka Object serilizer and deserializer

梦想的初衷 提交于 2019-12-11 15:08:32

问题


I am doing kafka producers and consumers. there are three ways to do Serialization and de-serialization.

1, custom object -> byte[] -> object (example)

2, custom object -> String -> object (I tried. working)

3, custom object -> JsonNode -> object (example)

Which one is better? Thanks


回答1:


You can try benchmarking but I would imagine all three are about the same.

  1. Object -> byte[]: In this case three things are happening.
    1. Object is converted into a tree of Json objects.
    2. Tree is converted to a string.
    3. String is converted to bytes.
  2. Object -> String: In this case the same three things are happening.
    1. I assume you are using the object mapper, which builds a tree of Json objects internally and converts the tree to a string.
    2. I also assume you are using the StringSerializer which lets kafka convert the string to bytes.
  3. Object -> JsonNode: Again same three things are happening.
    1. The object mapper in producing a tree of Json objects.
    2. The JsonSerializer converts the tree to a string and the string to bytes.

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.



来源:https://stackoverflow.com/questions/48550336/java-kafka-object-serilizer-and-deserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!