Search code examples
jsonserializationapache-flink

Efficient Flink Tuple6 -> JSON serializatation


I have a final datatype of Tuple6<Long, Long, Long, Long, Long, Map<String, Integer>> that I want to serialize into JSON and sink into a Kafka topic, right now I am using SimpleJson but I feel like it can be optimized.

My current code to serialize looks like this:

@Override
public byte[] serialize(Tuple6<Long, Long, Long, Long, Long, Map<String, Integer>> value) {
  JSONObject json = new JSONObject();
  json.put("key1", value.f0);
  json.put("key2", value.f1);
  json.put("key3", value.f2);
  json.put("key4", value.f3);
  json.put("key5", value.f4);
  Iterator it = value.f5.entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry pairs = (Map.Entry) it.next();
    json.put(pairs.getKey(), pairs.getValue());
  }
  return json.toString().getBytes();
}

Looking into DSL Json, I can't quite figure out how to properly produce the byte[] as it was unclear to me. Is there a better way to produce the byte[] json so I don't have to rely on SimpleJson's toString() then getting the byte array?


Solution

  • The most optimal approach is to build the JSON yourself in a StringBuffer, and then convert that to bytes. Given the simple structure of your data, that's not going to be very hard.