Java Avro映射序列化/反序列化问题
目前,我正在使用AVRO1.8.0对对象进行序列化/反序列化,但特别是对java.util.Map对象,我面临着一个问题。不面临其他类型对象的问题 这里的示例代码-Java Avro映射序列化/反序列化问题,java,json,serialization,deserialization,avro,Java,Json,Serialization,Deserialization,Avro,目前,我正在使用AVRO1.8.0对对象进行序列化/反序列化,但特别是对java.util.Map对象,我面临着一个问题。不面临其他类型对象的问题 这里的示例代码- class AvroUtils { public byte[] serialize(Object payload) { final ByteArrayOutputStream out = new ByteArrayOutputStream(); Schema schema = new Ref
class AvroUtils {
public byte[] serialize(Object payload) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Schema schema = new ReflectDatumWriter().getData().induce(payload); //---> getting proper map schema as {"type":"map","values":"string"}
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, out);
final GenericDatumWriter<Object> writer = new GenericDatumWriter(schema);
writer.write(payload, jsonEncoder);
jsonEncoder.flush();
return out.toByteArray();
}
public <R> R deserialize(Object o, Class<R> aClass) {
Schema schema = new ReflectDatumWriter().getData().induce(o); //------> getting error - unable to get schema
final ByteArrayInputStream bin = new ByteArrayInputStream((byte[]) o);
JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, bin);
final GenericDatumReader<R> reader = new GenericDatumReader<>(schema);
return reader.read(null, jsonDecoder);
}
public static void main(String[] args) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("Key1", "Value1");
map.put("Key2", "Value2");
// Serialization
byte[] result = this.serialize(map);
System.out.println("Serialized Data : " + new String(mapDes, "UTF-8"));
// Deserialization
Map<String, Object> mapDes = (Map<String, Object>) this.deserialize(result, Map.class);
System.out.println("Deserialized Data : " + mapDes);
}
}
注意:最后,这两个方法将放在不同的库中(avro序列化程序/avro反序列化程序)
请建议在反序列化方法中获取架构的最佳方法
谢谢。您得到了
java.lang.ClassCastException:[B不能强制转换为java.util.Collection
,因为您试图用字节数组而不是映射的对象调用incluse()
方法
如果要在一个位置序列化映射,在另一个位置反序列化映射,可以使用更好的方法:
Schema schema = Schema.createMap(Schema.create(Schema.Type.STRING));
若这样做,那个么在Desiralize方法中就不需要任何额外的参数
另外,GenericDatumWriter
只能用于一般记录,因此您需要一个ReflectDatumWriter
下面是一个代码更改示例:
public class AvroUtils {
public static byte[] serialize(Object payload) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Schema schema = Schema.createMap(Schema.create(Schema.Type.STRING)); //---> getting proper map schema as {"type":"map","values":"string"}
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, out);
final DatumWriter<Object> writer = new ReflectDatumWriter<>(schema);
writer.write(payload, jsonEncoder);
jsonEncoder.flush();
return out.toByteArray();
}
public static <R> R deserialize(Object o) throws IOException {
Schema schema = Schema.createMap(Schema.create(Schema.Type.STRING));
JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, new ByteArrayInputStream((byte[]) o));
final DatumReader<R> reader = new ReflectDatumReader<>(schema);
return reader.read(null, jsonDecoder);
}
public static void main(String[] args) throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("Key1", "Value1");
map.put("Key2", "Value2");
// Serialization
byte[] result = serialize(map);
// Deserialization
Map<String, Object> mapDes = deserialize(result);
System.out.println("Deserialized Data : " + mapDes);
}
}
您将获得
java.lang.ClassCastException:[B无法强制转换为java.util.Collection
,因为您试图使用字节数组而非映射的对象调用include()
方法
如果要在一个位置序列化映射,在另一个位置反序列化映射,可以使用更好的方法:
Schema schema = Schema.createMap(Schema.create(Schema.Type.STRING));
若这样做,那个么在Desiralize方法中就不需要任何额外的参数
另外,GenericDatumWriter
只能用于一般记录,因此您需要一个ReflectDatumWriter
下面是一个代码更改示例:
public class AvroUtils {
public static byte[] serialize(Object payload) throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Schema schema = Schema.createMap(Schema.create(Schema.Type.STRING)); //---> getting proper map schema as {"type":"map","values":"string"}
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(schema, out);
final DatumWriter<Object> writer = new ReflectDatumWriter<>(schema);
writer.write(payload, jsonEncoder);
jsonEncoder.flush();
return out.toByteArray();
}
public static <R> R deserialize(Object o) throws IOException {
Schema schema = Schema.createMap(Schema.create(Schema.Type.STRING));
JsonDecoder jsonDecoder = DecoderFactory.get().jsonDecoder(schema, new ByteArrayInputStream((byte[]) o));
final DatumReader<R> reader = new ReflectDatumReader<>(schema);
return reader.read(null, jsonDecoder);
}
public static void main(String[] args) throws IOException {
Map<String, Object> map = new HashMap<>();
map.put("Key1", "Value1");
map.put("Key2", "Value2");
// Serialization
byte[] result = serialize(map);
// Deserialization
Map<String, Object> mapDes = deserialize(result);
System.out.println("Deserialized Data : " + mapDes);
}
}
你好@DontPanic,你能回答我的问题吗:@ankit我会试试的@DontPanic,你能回答我的问题吗:@ankit我会试试的