Home > Net >  How to map types using properties file in Kafka
How to map types using properties file in Kafka

Time:12-03

I have 2 spring boot applications. I am trying to map in the application.properties file in such way that the Consumer can receive the message send by the producer. I want to add that I am using a CustomMessage:

public class CustomMessage {

   private LocalDateTime timestamp;
   private Integer sensor_id;
   private Double measurement_value;
// getters and setters
}

My application.properties file for the producer:

# other properties ( I use CloudKarafka )
spring.kafka.producer.key-serializer= org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer= org.springframework.kafka.support.serializer.JsonSerializer

spring.kafka.producer.properties.spring.json.type.mapping=customMessage:assignment2.kafka.CustomMessage,customMessage:ro.tuc.ds2020.kafkaconsumer.CustomMessage

And for consumer:

spring.kafka.consumer.key-deserializer= org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer= org.springframework.kafka.support.serializer.JsonDeserializer
spring.kafka.consumer.properties.spring.json.type.mapping=customMessage:ro.tuc.ds2020.kafkaconsumer.CustomMessage, customMessage:assignment.kafka.CustomMessage

I am not sure how the type.mapping should be done. Doing in this way presented, I get the following error:

Failed to construct kafka consumer
Failed to load: assignment.kafka.CustomMessage for  customMessage

It is also failing to construct producer.

CodePudding user response:

The map is from a class to a token (producer side) and token to class (consumer side).

You are mapping 2 different types to the same token.

Producer:

customMessage:assignment2.kafka.CustomMessage,
customMessage:ro.tuc.ds2020.kafkaconsumer.CustomMessage

Consumer:

customMessage:ro.tuc.ds2020.kafkaconsumer.CustomMessage, 
customMessage:assignment.kafka.CustomMessage

Assuming the producer's class is assignment2.kafka.CustomMessage you just need

customMessage:assignment2.kafka.CustomMessage

on the producer side and

customMessage:ro.tuc.ds2020.kafkaconsumer.CustomMessage

on the consumer side.

  • Related