自定义的Kafka反序列化器忽略了空消息。

huangapple go评论89阅读模式
英文:

Custom Kafka Deserializer ignores null messages

问题

我正在使用Kotlin Spring Boot,并且我有一个自定义的Kafka反序列化器。问题是,自定义的Deserializer完全忽略了具有空值的消息。是否有一种方法让空消息通过deserialize方法?

我正在在application.yaml上设置自定义Deserialiserlizer

spring:
  kafka:
    consumer:
      value-deserializer: mypackage.CustomDeserializer
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer

而CustomDeserializer如下

class CustomDeserializer : Deserializer<Result<MyData?>> {
    private val log = logger()

    override fun deserialize(topic: String?, data: ByteArray?): Result<MyData?> {
    //反序列化逻辑
    }
}
英文:

I'm using Kotlin Spring Boot and I have a custom Kafka Deserialiser. The issue is that the custom Deserializer complete ignores messages that have null values. Is there a way for null messages to go through the deserialize method?

I'm setting the custom Deserialiserlizer on application.yaml

spring:
  kafka:
    consumer:
      value-deserializer: mypackage.CustomDeserializer
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer

And the CustomDeserializer is like

class CustomDeserializer : Deserializer&lt;Result&lt;MyData?&gt;&gt; {
    private val log = logger()

    override fun deserialize(topic: String?, data: ByteArray?): Result&lt; MyData?&gt; {
    //deserialisation logic
    }
}

答案1

得分: 1

这不是预期的行为。查看 Kafka 源代码 可接受空字节数组。

也许你确实需要在Java中实现这个类,因为ByteArray?byte[]可能不会编译成相同的值?

另外,正如评论中提到的,你可以使用基于Avro、Protobuf或JSON数据的现有反序列化器。

英文:

This is not expected behavior. See Kafka Source Code that does accept null byte arrays.

Perhaps you do need to implement the class in Java since ByteArray? and byte[] may not compile to the same value?

Alternatively, as mentioned in the comments, you could use existing Deserializers that are based on Avro, Protobuf, or JSON data.

huangapple
  • 本文由 发表于 2023年6月1日 15:30:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定