简体   繁体   English

使用 Map 反序列化 Avro 中的对象<String,Object>字段返回具有错误类的值

[英]Deserializing objects in Avro with Map<String,Object> field returns values with wrong class

Trying to serialize objects that contain a Map instance in Apache Avro and the string keys of the Map are being deserialized but values are deserialized as class Object.尝试在 Apache Avro 中序列化包含 Map 实例的对象,并且 Map 的字符串键被反序列化,但值被反序列化为类 Object。

Able to use a GenericDatumWriter with a GenericData.Record instance with the properties copied into it but need to serialize the objects directly without having to copy the Map properties into a temporary object just to serialize it.能够将GenericDatumWriterGenericData.Record实例一起使用,并将属性复制到其中,但需要直接序列化对象,而不必将 Map 属性复制到临时对象中只是为了序列化它。

public void test1() {

    TimeDot dot = new TimeDot();
    dot.lat = 12;
    dot.lon = 34;
    dot.putProperty("id", 1234);
    dot.putProperty("s", "foo");
    System.out.println("BEFORE: " + dot);

    // serialize
    ReflectDatumWriter<TimeDot> reflectDatumWriter = new ReflectDatumWriter<>(TimeDot.class);
    Schema schema = ReflectData.get().getSchema(TimeDot.class);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataFileWriter<TimeDot> writer = new DataFileWriter<>(reflectDatumWriter).create(schema, out);
    writer.append(dot);
    writer.close();

    // deserialize
    ReflectDatumReader<TimeDot> reflectDatumReader = new ReflectDatumReader<>(TimeDot.class);
    ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
    DataFileStream<TimeDot> reader = new DataFileStream<>(inputStream, reflectDatumReader);
    Object dot2 = reader.next();
    reader.close();
    System.out.println("AFTER: " + dot2);
}

public static class TimeDot {
    Map<String, Object> props = new LinkedHashMap<>();
    double lat;
    double lon;

    public void putProperty(String key, Object value) {
        props.put(key, value);
    }

    public String toString() {
        return "lat="+ lat +", lon="+ lon +", props="+props;
    }
}

Output:输出:

 BEFORE: lat=12.0, lon=34.0, props={id=1234, s=foo}

 AFTER:  lat=12.0, lon=34.0, props={id=java.lang.Object@2b9627bc, s=java.lang.Object@65e2dbf3}

Next tried to manually create the Schema but that fails to serialize.接下来尝试手动创建架构,但无法序列化。

Exception in thread "main" java.lang.NullPointerException: in TimeDot in map in java.lang.Object null of java.lang.Object of map in field props of TimeDot线程“main”中的异常 java.lang.NullPointerException: in TimeDot in map in java.lang.Object null of java.lang.Object of map in the field props in TimeDot

public void test2() throws IOException {        

    TimeDot dot = new TimeDot();
    dot.lat = 12;
    dot.lon = 34;
    dot.putProperty("id", 1234);
    dot.putProperty("s", "foo");
    System.out.println(dot);

    // create Schema
    List<Schema.Field> propFields = new ArrayList<>();
    propFields.add(new Schema.Field("id", Schema.create(Schema.Type.INT)));
    propFields.add(new Schema.Field("s", Schema.create(Schema.Type.STRING)));
    Schema propRecSchema = Schema.createRecord("Object",null,"java.lang",false,propFields);
    Schema propSchema = Schema.createMap(propRecSchema);
    List<Schema.Field> fields = new ArrayList<>(3);
    fields.add(new Schema.Field("lat", Schema.create(Schema.Type.DOUBLE)));
    fields.add(new Schema.Field("lon", Schema.create(Schema.Type.DOUBLE)));
    fields.add(new Schema.Field("props", propSchema));
    Schema schema = Schema.createRecord("TimeDot", null, "", false, fields);
    System.out.println("\nschema:\n" + schema);

    // serialize
    ReflectDatumWriter<TimeDot> reflectDatumWriter = new ReflectDatumWriter<>(TimeDot.class);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataFileWriter<TimeDot> writer = new DataFileWriter<>(reflectDatumWriter).create(schema, out);
    writer.append(dot); // *** fails here > NullPointerException ***
    writer.close();

    // deserialize
    ReflectDatumReader<TimeDot> reader = new ReflectDatumReader<>(schema);
    TimeDot dot2 = reader.read(null,
            DecoderFactory.get().binaryDecoder(out.toByteArray(), null));
    System.out.println(dot2);
}

I think the easiest way is to add an annotation我认为最简单的方法是添加注释

@org.apache.avro.reflect.AvroSchema("{\"type\": \"map\", \"values\": [\"string\", \"int\"]}")
Map<String, Object> props = new LinkedHashMap<>();

To serialize an object that contains a Map must define a Union in the Avro schema with the list of all possible types of values.要序列化包含 Map 的对象,必须在 Avro 模式中定义一个联合,其中包含所有可能类型的值的列表。

IMPORTANT: If do not set the namespace correctly then the deserialization returns a GenericData.Record rather than a TimeDot class instance.重要提示:如果没有正确设置命名空间,则反序列化将返回 GenericData.Record 而不是 TimeDot 类实例。

    List<Schema.Field> fields = new ArrayList<>();
    fields.add(new Schema.Field("lat", Schema.create(Schema.Type.DOUBLE)));
    fields.add(new Schema.Field("lon", Schema.create(Schema.Type.DOUBLE)));
    fields.add(new Schema.Field("props", Schema.createMap(
            Schema.createUnion(Arrays.asList(
                Schema.create(Schema.Type.INT),
                Schema.create(Schema.Type.STRING))))));

    Schema schema = Schema.createRecord("TimeDot", null, "TestAvroUnion", false, fields);

    TimeDot dot = new TimeDot();
    dot.lat = 12;
    dot.lon = 34;
    dot.putProperty("id", 1234);
    dot.putProperty("s", "foo");
    System.out.println("BEFORE: " + dot);

    // serialize
    ReflectDatumWriter<TimeDot> reflectDatumWriter = new ReflectDatumWriter<>(schema);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DataFileWriter<TimeDot> dataWriter = new DataFileWriter<>(reflectDatumWriter);
    dataWriter.create(schema, out);
    dataWriter.append(dot);
    dataWriter.close();

    // deserialize
    ReflectDatumReader<TimeDot> reflectDatumReader = new ReflectDatumReader<>(schema);
    try(
        ByteArrayInputStream bis = new ByteArrayInputStream(out.toByteArray());
        DataFileStream<TimeDot> reader = new DataFileStream<>(bis, reflectDatumReader)
    ) {
        TimeDot dot2 = reader.next();
        System.out.println("AFTER:  " + dot2);
    }
}

The output is as follows:输出如下:

 BEFORE: lat=12.0, lon=34.0, props={id=1234, s=foo}
 AFTER:  lat=12.0, lon=34.0, props={id=1234, s=foo}

Alternatively use SchemaBuilder to create the schema:或者使用SchemaBuilder创建架构:

 Schema schema = SchemaBuilder
            .record("TimeDot")
            .namespace("TestUnion")
            .fields()
            .name("lat")
                .type().doubleType()
                .noDefault()
            .name("lon")
                .type().doubleType()
                .noDefault()
            .name("props")
                .type().map()
                    .values(SchemaBuilder.unionOf().intType().and().stringType().endUnion())
                .noDefault()
            .endRecord();

错误的反序列化列表<object>与 gson<div id="text_translate"><p> 我用List&lt;Object&gt;调试CUSTOMER object:</p><p> <a href="https://i.stack.imgur.com/Gh2Mx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gh2Mx.png" alt="在此处输入图像描述"></a></p><p> 这是 CUSTOMER.class:</p><pre> public class CUSTOMER { @XmlElements({ @XmlElement(name = "ADDRESS", type = ADDRESS.class), @XmlElement(name = "ZIP", type = ZIP.class), @XmlElement(name = "CITY", type = CITY.class), @XmlElement(name = "COUNTRY", type = COUNTRY.class), }) protected List&lt;Object&gt; addressAndZIPAndCITY; // other fields }</pre><p> 但是当我反序列化并从中创建 json 时,它仅包含:</p><pre> { "addressAndZIPAndCITY": [ { "value": "some value", "type": "some type" }, { "value": "some value 2", "type": "some type 2" }] }</pre><p> 缺少 ADRESS、ZIP、CITY 和 COUNTRY 对象标题。 所以反序列化不好。</p><p> 我无法更改List&lt;Object&gt;声明。 是否可以选择将其反序列化为带有 ADRESS、ZIP、CITY 等的 json? 像这样:</p><pre> { "addressAndZIPAndCITY": [{ "ADDRESS": { "value": "some value", "type": "some type" } }, { "ZIP": { "value": "some value 2", "type": "some type 2" } } ] }</pre><p> 编辑:我的意思是它看起来像 GSON 不知道哪个 object 在那里,但可能你知道如何向 model 添加一些注释或其他东西来识别它?</p></div></object> - Wrong deserializing List<Object> with gson

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Gson反序列化Map <String,Object>字段 - Deserializing a Map<String, Object> field with Gson Java:将JSON结构反序列化为Map <String, Object> - Java: Deserializing JSON structure to Map<String, Object> 使用GSON将JSON对象反序列化为字符串字段 - Deserializing JSON object to string field with GSON 使用不同的字段名称在Java类上映射Avro文件 - Map Avro files on Java class with different field names Avro 字段默认值 - Avro field default values 忽略 Map 中的字段<string, object>在测试时是 class 的成员</string,> - Ignoring a field in Map<String, Object> that is a member of a class while testing 反序列化地图 <Object, Object> 与GSon - Deserializing Map<Object, Object> with GSon 错误的反序列化列表<object>与 gson<div id="text_translate"><p> 我用List&lt;Object&gt;调试CUSTOMER object:</p><p> <a href="https://i.stack.imgur.com/Gh2Mx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gh2Mx.png" alt="在此处输入图像描述"></a></p><p> 这是 CUSTOMER.class:</p><pre> public class CUSTOMER { @XmlElements({ @XmlElement(name = "ADDRESS", type = ADDRESS.class), @XmlElement(name = "ZIP", type = ZIP.class), @XmlElement(name = "CITY", type = CITY.class), @XmlElement(name = "COUNTRY", type = COUNTRY.class), }) protected List&lt;Object&gt; addressAndZIPAndCITY; // other fields }</pre><p> 但是当我反序列化并从中创建 json 时,它仅包含:</p><pre> { "addressAndZIPAndCITY": [ { "value": "some value", "type": "some type" }, { "value": "some value 2", "type": "some type 2" }] }</pre><p> 缺少 ADRESS、ZIP、CITY 和 COUNTRY 对象标题。 所以反序列化不好。</p><p> 我无法更改List&lt;Object&gt;声明。 是否可以选择将其反序列化为带有 ADRESS、ZIP、CITY 等的 json? 像这样:</p><pre> { "addressAndZIPAndCITY": [{ "ADDRESS": { "value": "some value", "type": "some type" } }, { "ZIP": { "value": "some value 2", "type": "some type 2" } } ] }</pre><p> 编辑:我的意思是它看起来像 GSON 不知道哪个 object 在那里,但可能你知道如何向 model 添加一些注释或其他东西来识别它?</p></div></object> - Wrong deserializing List<Object> with gson Jackson:转换 JSON 代表 Map<string,object> 单场 POJO 到 Map<string,string> 字段值?</string,string></string,object> - Jackson: convert JSON representing a Map<String,Object> of single-field POJO's to a Map<String,String> of field values? 反序列化返回 null object Java - Deserializing returns null object Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM