AVRO GenericDatumWriter在编写ComplexType数据时失败

时间:2015-05-22 13:19:58

标签: java parsing avro

这是我的情景: 我的课程模式由Avro验证:

{
"type": "record",
"name": "MyCLass",
"namespace": "com.somepackage",
"fields": [
    {
        "name": "attributes",
        "type": {
            "type": "array",
            "items": {
                "type": "record",
                "name": "KeyValuePair",
                "fields": [
                    {
                        "name": "key",
                        "type": "string"
                    },
                    {
                        "name": "value",
                        "type": "string"
                    }
                ]
            },
            "java-class": "java.util.List"
        }
    },
    {
        "name": "someString",
        "type": "string"
    },
    {
        "name": "myclass1",
        "type": {
            "type": "record",
            "name": "MyClass1",
            "fields": [
                {
                    "name": "attributes",
                    "type": {
                        "type": "record",
                        "name": "ArrayOfKeyValuePair",
                        "fields": [
                            {
                                "name": "item",
                                "type": {
                                    "type": "array",
                                    "items": "KeyValuePair",
                                    "java-class": "java.util.List"
                                }
                            }
                        ]
                    }
                },
                {
                    "name": "labels",
                    "type": {
                        "type": "record",
                        "name": "ArrayOfXsdString",
                        "fields": [
                            {
                                "name": "item",
                                "type": {
                                    "type": "array",
                                    "items": "string",
                                    "java-class": "java.util.List"
                                }
                            }
                        ]
                    }
                },
                {
                    "name": "uniqueID",
                    "type": "string"
                }
            ]
        }
    },
    {
        "name": "MyClass2",
        "type": {
            "type": "record",
            "name": "MyClass2",
            "fields": [
                {
                    "name": "attributes",
                    "type": "ArrayOfKeyValuePair"
                },
                {
                    "name": "someString",
                    "type": "string"
                },
                {
                    "name": "someLabel",
                    "type": "ArrayOfXsdString"
                },
                {
                    "name": "someId",
                    "type": "string"
                }
            ]
        }
    }
]
}

使用`GenericRecord obj = new GenericData.Record(ReflectData.get()。getSchema(MyClass.class));

生成此模式

接下来,我构建了obj在其值列表中预期的4个参数列表:

KeyValuePair kv1 = new KeyValuePair();
    kv1.setKey("key1");
    kv1.setValue("val1");
    KeyValuePair kv2 = new KeyValuePair();
    kv2.setKey("key2");
    kv2.setValue("val2");

    List<KeyValuePair> attr = new ArrayList<KeyValuePair>();
    attr.add(kv1);
    attr.add(kv2);

    ArrayOfKeyValuePair arrKV = new ArrayOfKeyValuePair();
    arrKV.getItem().add(kv1);
    MyClass1 ud = new MyClass1();
    ud.setAttributes(arrKV);
    ud.setUniqueID("SomeID");

    MyCLass2 sd = new MyCLass2();
    sd.setAttributes(arrKV);
    sd.setExternalCaseID("SomeID");
    sd.setUniqueID("SomeId");

    obj.put("attributes", attr);
    obj.put("workFlowName", "Nume workflow");
    obj.put("userDescriptor", ud);
    obj.put("subscriberDescriptor", sd);

当我尝试时:

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DatumWriter<GenericRecord> writerS = new      GenericDatumWriter<GenericRecord>(schemaMentionedAbove);
    Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);
    writerS.write(obj, encoder);
    encoder.flush();
    out.close();

代码在行失败:writerS.write(obj,encoder);有错误: KeyValuePair无法强制转换为org.apache.avro.generic.IndexedRecord

KeyValuePair类是一个包含2个字段的简单类:String key,String value。

调试后,我可以看到DatumWriter在迭代第一个&#34;属性时失败了#34;一系列记录。

任何帮助表示赞赏!感谢

0 个答案:

没有答案