avro反序列化错误

gojuced7  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(446)

我有两个模式:
方案1(旧方案):

{
    "namespace": "com.org.package",
    "type": "record",
    "name": "EventModel",
    "fields": [
                    {
                        "name":"name",
                        "type":"string"
                    },
                    {
                        "name":"id",
                        "type":"long"
                    }
]
}

我用一个布尔字段更新了架构:
方案2(新方案):

{
    "namespace": "com.org.package",
    "type": "record",
    "name": "EventModel",
    "fields": [
                    {
                        "name":"name",
                        "type":"string"
                    },
                    {
                        "name":"id",
                        "type":"long"
                    },
                    {
                        "name":"isActive",
                        "type":"boolean",
                        "default":false
                    }
]
}

kafka主题包含属于旧模式(schema1)的消息。更新使用者架构后,使用者即使在updated字段中存在默认值,也无法反序列化旧的架构消息。
根据avro文件:

if the reader's record schema has a field that contains a default value, and writer's schema does not have a field with the same name, then the reader should use the default value from its field.
if the reader's record schema has a field with no default value, and writer's schema does not have a field with the same name, an error is signalled.

avro文件
反序列化时出现以下错误:

java.io.EOFException: null
    at org.apache.avro.io.BinaryDecoder.readBoolean(BinaryDecoder.java:140) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.io.ValidatingDecoder.readBoolean(ValidatingDecoder.java:77) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:194) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.specific.SpecificDatumReader.readField(SpecificDatumReader.java:136) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:237) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.specific.SpecificDatumReader.readRecord(SpecificDatumReader.java:123) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:170) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151) ~[avro-1.9.1.jar!/:1.9.1]
    at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:144) ~[avro-1.9.1.jar!/:1.9.1]

当记录缺少字段时,为什么默认值不应用于使用者?非常感谢您的帮助。提前谢谢!

kmynzznz

kmynzznz1#

尝试将类型更改为 boolean 以及 null 为了 isActive 而不仅仅是 boolean . 比如:

{
    "name": "isActive",
    "type": ["boolean", "null"],
    "default": false
}

它将使模式向后兼容。

相关问题