1

I have two Kafka topics in my consumer application. How can I configure one topic to use StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer) and other one to use KafkaAvroDeserializer (io.confluent.kafka.serializers.KafkaAvroDeserializer)?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Kstackr
  • 9
  • 2
  • 1
    Does this answer your question? [Using multiple deserializers for a kafka consumer](https://stackoverflow.com/questions/40218789/using-multiple-deserializers-for-a-kafka-consumer) – hermit Aug 11 '23 at 11:56
  • 2
    The best and simplest way to achieve this is to configure and use two consumers. – Mar-Z Aug 11 '23 at 12:24
  • What have you tried so far? Deserializers are associated with consumers, not topics. So create two of them – OneCricketeer Aug 11 '23 at 12:54
  • Spring provides a `DelegatingByTopicDeserializer`. – Gary Russell Aug 23 '23 at 19:51

1 Answers1

1

Use the DelegatingByTopicDeserializer.

https://docs.spring.io/spring-kafka/docs/current/reference/html/#by-topic

Starting with version 2.8, the DelegatingByTopicSerializer and DelegatingByTopicDeserializer allow selection of a serializer/deserializer based on the topic name. Regex Patterns are used to lookup the instance to use.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179