英文:
Avro - Unable to serialize payload org.apache.avro.AvroRuntimeException: Unknown datum type java.math.BigDecimal: 8.32
问题
我正在编写单元测试,针对一个发送事件到Kafka的方法。
在Avro模式中,我已经定义了一个字段:
union { decimal(10, 2), null } cost
它在Java中表示为BigDecimal。
在单元测试中,我正在使用DatumReader来读取是否正确地将事件发送到了主题。
private final DatumReader<Event> eventReader = new SpecificDatumReader<>(Event.class);
// 从主题中读取消息
final ConsumerRecord<String, byte[]> eventMessage = KafkaTestUtils.getSingleRecord(
kafkaConsumer, eventTopic, ofSeconds(10).toMillis()
);
final Event event = eventReader.read(
null,
DecoderFactory.get().binaryDecoder(eventMessage.value(), null)
);
最后,我收到了一个错误消息:
无法序列化负载
org.apache.avro.AvroRuntimeException: 未知的数据类型 java.math.BigDecimal: 8.32
有人知道如何修复吗?
英文:
I'm writing unit test to one of the method which is sending event to kafka.
In avro schema I have defined filed
union { decimal(10, 2), null } cost
which is represented in Java as BigDecimal.
In the unit test I'm using DatumReader for reading if event was correctly sent to topic.
private final DatumReader<Event> eventReader = new SpecificDatumReader<>(Event.class);
//reading message from topic
final ConsumerRecord<String, byte[]> eventMessage = KafkaTestUtils.getSingleRecord(
kafkaConsumer, eventTopic, ofSeconds(10).toMillis()
);
final Event event = eventReader.read(
null,
DecoderFactory.get().binaryDecoder(eventMessage.value(), null)
);
And finally I'm receiving an error
> Unable to serialize payload
org.apache.avro.AvroRuntimeException: Unknown datum type java.math.BigDecimal: 8.32
Is anyone know how to fix it?
答案1
得分: 0
Finally adding
GenericData.get().addLogicalTypeConversion(new Conversions.DecimalConversion());
before calling read() method
final Event event = eventReader.read(
null,
DecoderFactory.get().binaryDecoder(eventMessage.value(), null)
);
fixed the problem.
英文:
Finally adding
GenericData.get().addLogicalTypeConversion(new Conversions.DecimalConversion());
before calling read() method
final Event event = eventReader.read(
null,
DecoderFactory.get().binaryDecoder(eventMessage.value(), null)
);
fixed the problem.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论