我使用以下代码在GenericRecord和字节列表之间进行序列化和反序列化:
public static byte[] encode(List<GenericRecord> records, Schema schema) throws IOException {
SpecificDatumWriter<GenericRecord>
datumWriter =
new SpecificDatumWriter<>(schema);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.reset();
BinaryEncoder binaryEncoder = new EncoderFactory().binaryEncoder(byteArrayOutputStream, null);
for(GenericRecord segment: records) {
datumWriter.write(segment, binaryEncoder);
}
binaryEncoder.flush();
byte[] bytes = byteArrayOutputStream.toByteArray();
return bytes;
}
public static List<GenericRecord> decode(byte[] recordBytes, Schema schema) throws IOException {
DatumReader<GenericRecord>
datumReader =
new SpecificDatumReader<>(schema);
ByteArrayInputStream stream = new ByteArrayInputStream(recordBytes);
stream.reset();
BinaryDecoder binaryDecoder = new DecoderFactory().binaryDecoder(stream, null);
List<GenericRecord> records = new ArrayList<>();
while (true) {
try {
GenericRecord record = datumReader.read(null, binaryDecoder);
records.add(record);
} catch (EOFException e) {
break;
}
}
return records;
}
我得到了以下异常:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -40
at org.apache.avro.io.parsing.Symbol$Alternative.getSymbol(Symbol.java:402)
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.read(GenericDatumReader.java:155)
at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:193)
at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151)
at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142)
at com.yahoo.gup.utils.SerilizationUtils.decode(SerilizationUtils.java:60)
根本原因的任何想法?我是否以正确的方式进行序列化?