我有一个no-sql db,用于存储序列化的avro对象。我使用sbt-avro插件专门生成Java类addSbtPlugin("com.cavorite" % "sbt-avro-1-8" % "1.1.5")
。现在,我想向架构中添加新字段,还希望与已经存在的数据向后兼容。但是使用新模式对旧数据进行反序列化会抛出java.io.EOFException
旧模式:
{
"name": "DNode",
"namespace": "my.model",
"type": "record",
"fields": [
{"name": "prev", "type": ["null", "string"]},
{"name": "next", "type": ["null", "string"]}
]
}
新架构:
{
"name": "DNode",
"namespace": "my.model",
"type": "record",
"fields": [
{"name": "prev", "type": ["null", "string"]},
{"name": "next", "type": ["null", "string"]},
{"name": "size", "type": ["null", "long"], "default": null}
]
}
反序列化代码
def deserialize(bytes: Array[Byte]) = {
val reader = new SpecificDatumReader[DNode](classOf[DNode])
val decoder = DecoderFactory.get().binaryDecoder(bytes, null)
reader.read(null, decoder)
}
投掷
java.io.EOFException
at org.apache.avro.io.BinaryDecoder.ensureBounds(BinaryDecoder.java:473)
at org.apache.avro.io.BinaryDecoder.readInt(BinaryDecoder.java:128)
at org.apache.avro.io.BinaryDecoder.readIndex(BinaryDecoder.java:423)
at org.apache.avro.io.ResolvingDecoder.doAction(ResolvingDecoder.java:290)
at org.apache.avro.io.parsing.Parser.advance(Parser.java:88)
at org.apache.avro.io.ResolvingDecoder.readIndex(ResolvingDecoder.java:267)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:179)
at org.apache.avro.specific.SpecificDatumReader.readField(SpecificDatumReader.java:116)
at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:222)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:175)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:153)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:145)
...