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

1

9

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"
         }
     ]
}
Godly answered 14/4, 2016 at 7:41 Comment(3)
might be a typo? In the schema the name space is com.xx but the exception writes com.xxx.File.Airily
I think it picks namespace with the "name" filed.Godly
@Airily i fixed this changing SpecificDatumReader to GenericDatumReader.Godly
G
2

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

Generable answered 12/1, 2022 at 7:38 Comment(2)
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.Molybdic
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 ReviewMymya

© 2022 - 2024 — McMap. All rights reserved.