我能够将我的java bean类作为avro记录发布到kafka。但是当我尝试消耗时,我得到了类强制转换异常。为什么会这样?
生产者
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);
消费
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);
异常
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架构
{
"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"
}
]
}
答案 0 :(得分:0)
基于评论者,问询者已经找到了解决方案:
我将更改后的SpecificDatumReader更改为GenericDatumReader