Apache Kafka: …StringDeserializer is not an instance of …Deserializer

前端 未结 3 1274

In my simple application i am trying to instantiate a KafkaConsumer my code is nearly a copy of the code from javadoc (\"Automatic Offset Committing\"):

@Slf4j
p         


        
相关标签:
3条回答
  • 2021-01-23 05:56

    Your Custom class need to implement, org.apache.kafka.common.serialization.Deserializer.

    like

    import org.apache.kafka.common.header.Headers;
    import org.apache.kafka.common.serialization.Serializer;
    import org.apache.kafka.common.serialization.Deserializer;
    import org.codehaus.jackson.map.ObjectMapper;
    
    import java.io.Serializable;
    import java.util.Map;
    
    //Developed by Arun Singh
    public class Employee implements Serializable, Serializer, **Deserializer** {
    
    @Override
        public Object deserialize(String s, byte[] bytes) {
            ObjectMapper mapper = new ObjectMapper();
            Employee employee = null;
            try {
                //employee = mapper.readValue(bytes, Employee.class);
                employee = mapper.readValue(bytes.toString(), Employee.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return employee;
        }
    
        @Override
        public Object deserialize(String topic, Headers headers, byte[] data) {
            ObjectMapper mapper = new ObjectMapper();
            Employee employee = null;
            try {
                //employee = mapper.readValue(bytes, Employee.class);
                employee = mapper.readValue(data.toString(), Employee.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return employee;
        }
    
        public void close() {
    
        }
    }
    
    0 讨论(0)
  • 2021-01-23 06:05

    Not sure if this is what finally fixed your error, but note that when using spring-kafka-test (version 2.1.x, starting with version 2.1.5) with the 1.1.x kafka-clients jar, you will need to override certain transitive dependencies as follows:

    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka</artifactId>
        <version>${spring.kafka.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.kafka</groupId>
        <artifactId>spring-kafka-test</artifactId>
        <version>${spring.kafka.version}</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.1.1</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.1.1</version>
        <classifier>test</classifier>
    </dependency>
    
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.11</artifactId>
        <version>1.1.1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.11</artifactId>
        <version>1.1.1</version>
        <classifier>test</classifier>
        <scope>test</scope>
    </dependency>
    

    so it could've been a problem with your transitive dependency for sure

    0 讨论(0)
  • 2021-01-23 06:08

    This might be the problem with Kafka classloading.
    Setting classloader to null might help.

    ...
    Thread currentThread = Thread.currentThread();    
    ClassLoader savedClassLoader = currentThread.getContextClassLoader();
    
    currentThread.setContextClassLoader(null);
    KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
    
    currentThread.setContextClassLoader(savedClassLoader);
    ...
    

    There is full explanation:
    https://stackoverflow.com/a/50981469/1673775

    0 讨论(0)
提交回复
热议问题