简体   繁体   English

处理模式注册表deserialiser kafka时出现异常

[英]Exception while processing schema registry deserialiser kafka

I'm following the confluent link to post kafka messages on changes to mysql table. 我正在关注汇合链接,以便在更改mysql表时发布kafka消息。 When I try to consume this message from a springboot application, I'm getting the below exception. 当我尝试从springboot应用程序中使用此消息时,我得到以下异常。 How can I fix this so that I can read the message. 我该如何解决这个问题,以便我可以阅读该消息。 Sometimes I am able to read the message but I get a serialised version if like consumed message is key: null value : 2foo 䶰Z 䶰Z 有时我能够阅读消息,但如果consumed message is key: null value : 2foo 䶰Z 䶰Z话,我会得到序列化版本consumed message is key: null value : 2foo 䶰Z 䶰Z

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.spring.kafkaexample.springbootkafkaconsumer.model.Foobar
    at com.spring.kafkaexample.springbootkafkaconsumer.listener.KafkaConsumer.consume(KafkaConsumer.java:22) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_131]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_131]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_131]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_131]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:181) ~[spring-messaging-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.messaging.handler.invocation.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:114) ~[spring-messaging-5.0.8.RELEASE.jar:5.0.8.RELEASE]

Below is my code 以下是我的代码

@KafkaListener(topics = "mysql-foobar", groupId = "group_id")
public void consume(ConsumerRecord<String, Foobar> message) {

    System.out.println("consumed message is key: "+message.key() + " value :  "+message.value());
}

Deserializer 解串器

@Bean
public Map<String, Object> consumerConfigs() {
    Map<String, Object> props = new HashMap<>();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class);
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "avro");

    return props;
}

If you are getting String cannot be cast to com.spring.kafkaexample.springbootkafkaconsumer.model.Foobar , then that means you have a producer sending a string into your topic, which is not a valid to be made a Foobar instance. 如果你得到String cannot be cast to com.spring.kafkaexample.springbootkafkaconsumer.model.Foobar ,那么这意味着你有一个生产者向你的主题发送一个字符串,这不是一个有效的Foobar实例。

If you are getting the 2foo 䶰Z 䶰Z stuff, then that would mean you're printing out the UTF-8 raw bytes of some binary data, such as Avro, and you would be better off extracting the fields of the object rather than relying solely on the implicit toString method. 如果你得到2foo 䶰Z 䶰Z东西,那就意味着你要打印掉一些二进制数据的UTF-8原始字节,比如Avro,你最好不要提取字段。对象而不是仅仅依赖于隐式的toString方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM