Java Kafka Object serilizer and deserializer

2019-08-17 18:40发布

问题:

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.