I'm new to Kafka, Serialization and JSON
WHAT I want the producer to send the JSON file through kafka and the consumer in order to consume and work with the JSON file in its original form.
I was able to get this, so JSON is a converter to string and sent via String Serializer, and then the consumer will parse the String and recreate the JSON object, but I'm worried that this is inefficient or the correct method (may lose field types for JSON)
So, I decided to create a JSON serializer and install it in my manufacturer configurations.
I used JsonEncoder here: Kafka: custom serializer entry
But when I try to run my producer now, it seems that in the toBytes function of the encoder, the try block never returns anything as I want it to
try { bytes = objectMapper.writeValueAsString(object).getBytes(); } catch (JsonProcessingException e) { logger.error(String.format("Json processing failed for object: %s", object.getClass().getName()), e); }
It seems objectMapper.writeValueAsString(object).getBytes() ; takes my JSON obj ( {"name":"Kate","age":25} ) and converts it to nothing,
this is my launch launcher function
List<KeyedMessage<String,JSONObject>> msgList=new ArrayList<KeyedMessage<String,JSONObject>>(); JSONObject record = new JSONObject(); record.put("name", "Kate"); record.put("age", 25); msgList.add(new KeyedMessage<String, JSONObject>(topic, record)); producer.send(msgList);
What am I missing? Will my original method (convert to string and send and then restore JSON obj), ok? or just not the right way?
THANKS!