Avro – Unable to serialize payload org.apache.avro.AvroRuntimeException: Unknown datum type java.math.BigDecimal: 8.32

huangapple go评论77阅读模式
英文:

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&lt;Event&gt; eventReader = new SpecificDatumReader&lt;&gt;(Event.class);

    //reading message from topic
    final ConsumerRecord&lt;String, byte[]&gt; 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.

huangapple
  • 本文由 发表于 2023年7月17日 18:07:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703402.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定