java.lang.ClassCastException:xx cannot be cast to org.apache.avro.generic.IndexedRecord

Ratha picture Ratha · Apr 14, 2016 · Viewed 10.4k times · Source

I was able to publish my java bean class as avro record to kafka. but when i try to consume i get class cast exception. Why this occurs?

producer

Schema schema = new Schema.Parser().parse(new File("/schemas/avro_schemas/test_schema.avsc"));

GenericRecord payload = new GenericData.Record(schema);
payload.put("name", fileName);
payload.put("timestamp", dateTime.toString());
payload.put("source", source);
payload.put("content", buf);
payload.put("customerCode", customercode); 
producer.publish(topic, payload, schema);

Consumer

ConsumerIterator<byte[], byte[]> it = stream.iterator();
while (it.hasNext()) {
try {
byte[] received_message = it.next().message();
Schema  schema = new Schema.Parser().parse(new File("/schemas/avro_schemas/test_schema.avsc"));
DatumReader<GenericRecord> reader = new SpecificDatumReader<GenericRecord>(schema);
Decoder decoder = DecoderFactory.get().binaryDecoder(received_message, null);
GenericRecord   payload = reader.read(null, decoder);

Exception

ava.lang.ClassCastException: com.xxx.File cannot be cast to org.apache.avro.generic.IndexedRecord

at org.apache.avro.generic.GenericData.setField(GenericData.java:573)

at org.apache.avro.generic.GenericData.setField(GenericData.java:590)

at org.apache.avro.generic.GenericDatumReader.readField(GenericDatumReader.java:193)

at org.apache.avro.generic.GenericDatumReader.readRecord(GenericDatumReader.java:183)

at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:151)

at org.apache.avro.generic.GenericDatumReader.read(GenericDatumReader.java:142)

at com.xxx.listener.KafkaMessageListenerThread.run(KafkaMessageListenerThread.java:56)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)

at java.util.

Avro Schema

{
    "namespace": "com.xx"
     "type": "record",
     "name": "File",
     "fields":[
         {
            "name": "name", "type": "string"
         },
         {
            "name": "timestamp",  "type": "string"
         },
         {
            "name": "source", "type": "string"
         },
         {
            "name": "content", "type": "bytes"
         },
         {
            "name": "customerCode", "type": "string"
         }
     ]
}

Answer

Dennis Jaheruddin picture Dennis Jaheruddin · Jul 15, 2019

Based on the comments the asker already found the solution:

i fixed this changing SpecificDatumReader to GenericDatumReader