avro-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Sameer Deokule <sdeok...@gmail.com>
Subject Exception while encoding generic record - java.lang.String cannot be cast to org.apache.avro.generic.IndexedRecord
Date Thu, 19 Jul 2012 19:36:19 GMT
Using  avro-1.6.2

I am encountering the following exception when I try to serialize a
constructed GenericRecord
using either the binary or json encoder . The schema compiles ok
(Schema.txt attachment to this email.)

Am I setting up one of the Java maps incorrectly?

java.lang.ClassCastException: java.lang.String cannot be cast to
org.apache.avro.generic.IndexedRecord
    at org.apache.avro.generic.GenericData.getField(GenericData.java:518)
    at org.apache.avro.generic.GenericData.getField(GenericData.java:533)
    at
org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:103)
    at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:65)
    at
org.apache.avro.generic.GenericDatumWriter.writeMap(GenericDatumWriter.java:165)
    at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:68)
    at
org.apache.avro.generic.GenericDatumWriter.writeRecord(GenericDatumWriter.java:105)
    at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:65)
    at
org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:57)

I was looking at TestGenericRecordBuilder.java in the avro github testsuite
and it
also seems to be using the builder.set to build the record.


GenericRecord is constructed as follows : (schema is created by parsing the
json file
for the schema attached )

                GenericRecord root = new GenericData.Record(schema);
                GenericRecordBuilder grb = new GenericRecordBuilder(schema);
                GenericRecord postBody;
                JSONObject kvm = new JSONObject(kvmStr);
                ObjectMapper mapper = new ObjectMapper();
                LinkedHashMap<CharSequence, CharSequence> _kvm =
mapper.readValue(kvm.toString().getBytes(),LinkedHashMap.class);
                LinkedHashMap<CharSequence, CharSequence> arguments = new
LinkedHashMap<CharSequence, CharSequence>();
                arguments.put("update_interval", "1");
                arguments.put("attr", "pr");
                arguments.put("keys", "10,pr");
                arguments.put("f", "/var/f");
                arguments.put("pause", "0");

                LinkedHashMap<CharSequence, Object> ud = new
LinkedHashMap<CharSequence, Object>();
                ud.put("type", "MapStringUD");


                LinkedHashMap<CharSequence, Object> data = new
LinkedHashMap<CharSequence, Object>();
                LinkedHashMap<CharSequence, Object> pr = new
LinkedHashMap<CharSequence, Object>();
                ud.put("data", data);
                data.put("pr", pr);
                pr.put("type", "ListInt");
                Integer [] iData = {1,2,3,4,5,6,7,8,9,10};
                List<Integer> lData = Arrays.asList(iData);
                pr.put("data", lData );

                List<LinkedHashMap<CharSequence,CharSequence>> requests =
Arrays.asList(new LinkedHashMap<CharSequence,CharSequence>());

                grb.set("requests", requests);
                grb.set("upd", upd);
                grb.set("kvm", _kvm);
                grb.set("context", new LinkedHashMap<CharSequence,
CharSequence>());
                grb.set("properties", new LinkedHashMap<CharSequence,
CharSequence>());
                grb.set("arguments", arguments);

                postBody = grb.build();
                JSONObject jj = new JSONObject(postBody.toString());
                System.out.println("json from avro is : ");
                System.out.println(jj.toString());
                json = jj;
                byte [] b = encodeJson(postBody);

GenericRecord.toString() shows the following :

{"requests":[{}],"arguments":{"pause":"0","keys":"10,pr","update_interval":"1","attr":"pr","f":"/var/f"},"context":{},"properties":{},"ud":{"data":{"pr":{"data":[1,2,3,4,5,6,7,8,9,10],"type":"ListInt"}},"type":"MapStringUD"},"kvm":{"K1":"VMFkNrrKcbRn","K2":"md&b=2","K3":"p=rsn1"}}

Schema is in attached file.

Thanks
Sameer

Mime
View raw message