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

14,188

Try org.apache.avro.reflect.ReflectDatumReader instead of org.apache.avro.specific.SpecificDatumReader.

Share:
14,188
Ratha
Author by

Ratha

Updated on June 21, 2022

Comments

  • Ratha
    Ratha almost 2 years

    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"
             }
         ]
    }
    
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
  • Andres Gardiol
    Andres Gardiol over 2 years
    This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review