Avro Map序列化/反序列化问题

时间:2017-06-17 22:38:22

标签: java json serialization deserialization avro

目前我正在使用 Avro 1.8.0 来序列化/反序列化对象,但特别针对java.util.Map对象面临问题。不会遇到其他类型对象的问题。

示例代码 -

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抛出错误 -

`Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to java.util.Collection
at org.apache.avro.reflect.ReflectData.getArrayAsCollection(ReflectData.java:196)
at org.apache.avro.generic.GenericData.induce(GenericData.java:612)`

注意:最后两种方法都将放在不同的库中(avro-serializer / avro-deserializer)。

请建议在反序列化方法中获取模式的最佳方法。

感谢。

1 个答案:

答案 0 :(得分:0)

您得到java.lang.ClassCastException: [B cannot be cast to java.util.Collection因为您尝试使用对象(字节数组而不是Map)调用induce()方法。

如果您想在一个地方序列化地图并在另一个地方反序列化,您可以使用更好的方式:

Schema schema = Schema.createMap(Schema.create(Schema.Type.STRING));

如果这样做,desirialize方法中不需要任何其他参数。

此外,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);
    }
}

结果,你会得到这样的东西:

Deserialized Data : {Key2=Value2, Key1=Value1}