org.json.simple.JSONObject cannot be cast to org.apache.avro.generic. IndexedRecord

973 views
Skip to first unread message

DC

unread,
Mar 14, 2018, 3:26:17 AM3/14/18
to Confluent Platform
When I am pushing a message from my kafka producer, My schema is getting registered in schema registry but it's giving me an error - 

org.apache.kafka.common.errors.SerializationException: Error serializing Avro message


Cause of this is - 

java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.apache.avro.generic.IndexedRecord


Here is my code  - 


private KafkaProducer<String, GenericRecord> kafkaProducer;


Schema.Parser parser = new Schema.Parser();

Schema schema = parser.parse(value.getSchema());

GenericRecord avroRecord = new GenericData.Record(schema);


avroRecord.put("ci", value.getCi());

avroRecord.put("cn", value.getCn());

avroRecord.put("n", value.getN());

        avroRecord.put("tg", value.getTg());

        avroRecord.put("ts", value.getTs());

        avroRecord.put("pl", value.getPl());

//In "value", value.getPl() is of type "T" but from where I am setting it, it's type is org.json.simple.JSONObject

ProducerRecord<String, GenericRecord> record = new ProducerRecord<String, GenericRecord>("test", avroRecord);

try {


kafkaProducer.send(record);

} catch (SerializationException e) {

// may need to do something with it

}


Please find the attached schema.

exampleSchemaICR.docx

Ewen Cheslack-Postava

unread,
Mar 14, 2018, 5:49:32 PM3/14/18
to Confluent Platform
JSONObject is not an Avro type, so the Avro library and the Avro serializer will not know how to handle it. What schema do you have for the pl field?

-Ewen

--
You received this message because you are subscribed to the Google Groups "Confluent Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to confluent-platform+unsub...@googlegroups.com.
To post to this group, send email to confluent-platform@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/confluent-platform/e2c20eba-47b6-482c-a63d-ebbcc7c764c1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

DC

unread,
Mar 15, 2018, 2:08:06 AM3/15/18
to Confluent Platform
Schema for pl is in attached file
To post to this group, send email to confluent...@googlegroups.com.

pranav....@oyorooms.com

unread,
Nov 28, 2018, 2:56:26 PM11/28/18
to Confluent Platform
{"type":"record","name":"hotelcrimes","namespace":"com.oyo","fields":[{"name":"_id","type":{"type":"record","name":"id","fields":[{"name":"oid","type":["null","string"]}]}},{"name":"bid","type":["null","int"]},{"name":"biv","type":["null","string"]},{"name":"cca","type":["null","string"]},{"name":"ct","type":["null","string"]},{"name":"hid","type":["null","int"]},{"name":"md","type":{"type":"record","name":"md","fields":[{"name":"feedback_id","type":["null","int"]},{"name":"remove_comment","type":["null","string"]},{"name":"shifting_id","type":["null","int"]},{"name":"ticket_id","type":["null","int"]}]}},{"name":"noc","type":["null","double"]},{"name":"oyo_id","type":["null","string"]},{"name":"r","type":["null","boolean"]},{"name":"st","type":["null","boolean"]},{"name":"ufc","type":["null","boolean"]},{"name":"updated_at","type":["null","string"]},{"name":"created_at","type":["null","string"]}]}

this is my schema, and I am hitting this issue, please assist
Reply all
Reply to author
Forward
0 new messages