我有一个byte[]
表示Avro有效负载和用于编码/解码此有效负载的模式。有几种方法可以使用ReflectDatumReader / Writer和SpecificDatumReader / Writer将此有效负载转换为SpecificRecord。
当我们使用SpecificDatumReader进行解码并尝试编码使用ReflectDatumReader返回的SpecificRecord时,我们会收到错误。
以下是示例代码。使用SpecificDatumReader获取SpecificRecord(测试),然后使用ReflectDatumWriter尝试将其编码为byte [],但失败。最后使用SpecificDatumReader重新解码它。
DatumWriter<Testing> refWriter = new ReflectDatumWriter<>(expectedSchema);
DatumReader<Testing> specReader = new SpecificDatumReader<>(expectedSchema);
// Use SpecificDatumReader to decode to a SpecificRecord
Testing r1 = specReader.read(null, DecoderFactory.get().binaryDecoder(expectedBytePayload, null));
// Use ReflectDatumWriter to write SpecificRecord encode as byte[].
// Note: this step fails
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
refWriter.write(r1, EncoderFactory.get().binaryEncoder(outStream, null));
// Use SpecificDatumReader to decode to a SpecificRecord
Testing r2 = specReader.read(null, DecoderFactory.get().binaryDecoder(outStream.toByteArray(), null));
错误讯息:
java.io.EOFException
at org.apache.avro.io.BinaryDecoder.ensureBounds(BinaryDecoder.java:473)
at org.apache.avro.io.BinaryDecoder.readDouble(BinaryDecoder.java:243)
at org.apache.avro.io.ResolvingDecoder.readDouble(ResolvingDecoder.java:190)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:185)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:240)
at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:230)
at org.apache.avro.generic.GenericDatumReader.readWithoutConversion(GenericDatumReader.java:174)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:152)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:144)