Описание вопроса:
Я хочу сериализовать GenericData.Record в файл. И десериализовать из файла. Я могу сериализовать успех. Но когда я десериализирую форму файла, проблема возникает.
Вот мой код:
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import org.apache.avro.Schema;
import org.apache.avro.generic.GenericData;
import org.objenesis.strategy.StdInstantiatorStrategy;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class KryoTest {
public static void main(String[] args) throws FileNotFoundException {
writeObj();
readObj();
}
public static void readObj() throws FileNotFoundException {
Kryo kryo = new Kryo();
kryo.register(GenericData.Record.class);
kryo.setInstantiatorStrategy(new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
Input input = new Input(new FileInputStream("file.bin"));
GenericData.Record object2 = kryo.readObject(input, GenericData.Record.class);
input.close();
}
public static void writeObj() throws FileNotFoundException {
final Schema schema = new Schema.Parser().parse("{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"myflink.serialize.bean\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"int\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}");
Kryo kryo = new Kryo();
kryo.register(GenericData.Record.class);
GenericData.Record record = new GenericData.Record(schema);
record.put("name", "chensi");
Output output = new Output(new FileOutputStream("file.bin"));
kryo.writeObject(output, record);
output.close();
}
}
, но я не могу десериализовать GenericData.Record со следующей трассировкой стека :
Exception in thread "main" com.esotericsoftware.kryo.KryoException: java.lang.UnsupportedOperationException
Serialization trace:
reserved (org.apache.avro.Schema$Field)
fieldMap (org.apache.avro.Schema$RecordSchema)
schema (org.apache.avro.generic.GenericData$Record)
at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:144)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:543)
at com.esotericsoftware.kryo.Kryo.readClassAndObject(Kryo.java:813)
at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:161)
at com.esotericsoftware.kryo.serializers.MapSerializer.read(MapSerializer.java:39)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:731)
at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:125)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:543)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:731)
at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:125)
at com.esotericsoftware.kryo.serializers.FieldSerializer.read(FieldSerializer.java:543)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:709)
at myflink.serialize.KryoTest.readObj(KryoTest.java:29)
at myflink.serialize.KryoTest.main(KryoTest.java:21)
Caused by: java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.add(Collections.java:1055)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:134)
at com.esotericsoftware.kryo.serializers.CollectionSerializer.read(CollectionSerializer.java:40)
at com.esotericsoftware.kryo.Kryo.readObject(Kryo.java:731)
at com.esotericsoftware.kryo.serializers.ObjectField.read(ObjectField.java:125)
... 13 more