Avro GenericRecord无法转换为Java.util.Collection问题

时间:2015-08-18 00:54:41

标签: avro

我在Avro很新。当我尝试序列化数据时,我在下面的代码中遇到了“writer.write(datum,encoder);”行中的异常。

        byte[] byteData = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try
        {             
            DatumWriter<GenericRecord> writer = new GenericDatumWriter<GenericRecord>(schema);  
            Encoder encoder = EncoderFactory.get().binaryEncoder(out, null);  
            writer.write(datum, encoder);  
            encoder.flush();  
            byteData = out.toByteArray();
        }
        catch (Exception e)
        {
            // TODO log error
            throw new RuntimeException (e.getMessage(), e);
        }
        finally
        {
            try
            {
                out.close(); 
            }
            catch (Exception e)
            {
                throw new RuntimeException (e.getMessage(), e);
            }
        }

        return byteData;

My Avro Schema

{"type":"record",
"name":"MainSchema",
"namespace":"****",
"fields":[{"name":"GenericName","type":"string"},
        {"name":"DataCarrier",
         "type":{"type":"array",
                "items":{
                            "type":"record",
                            "name":"SubSchema",
                            "fields":[{"name":"DataValue",
                                        "type":{"type":"map","values":"string"}}]}}}]}

错误信息如下:(对我来说,看起来avro编写器不喜欢那个GenericRecord数据数组)

java.lang.ClassCastException: [Lorg.apache.avro.generic.GenericRecord; cannot be cast to java.util.Collection

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

结果是UTF-8到String转换问题。解决。