我在kafka上收到protobuf消息,消费者被配置为使用
反序列化事件value.deserializer = org.apache.kafka.common.serialization.StringDeserializer
如果我通过传递反序列化事件字符串的字节数组来使用parseFrom(byte[] data)
com.google.protobuf.Parser
方法,则该方法抛出以下异常:
com.google.protobuf.InvalidProtocolBufferException: While parsing a protocol message, the input ended unexpectedly in the middle of a field. This could mean either than the input has been truncated or that an embedded message misreported its own length.
如果我改为使用
反序列化kafka事件value.deserializer = org.apache.kafka.common.serialization.ByteArrayDeserializer
并直接将接收到的字节数组传递给parseFrom
,正确解析protobuf,没有任何异常。
为什么第二种方式有效,但第一种方式不起作用?
答案 0 :(得分:1)
您正在使用字符串反序列化程序,它需要某些特殊字符来定义邮件的限制。它试图反序列化一个STRING,但他只收到一堆字符,其格式是消费者根本不期望的。
您有一个使用ByteArraySerializer
序列化的制作人,因此您的消费者必须使用ByteArrayDeserializer
对其进行反序列化。
如果您真的想以String格式自动反序列化,请尝试使用org.apache.kafka.common.serialization.StringSerializer
生成。