如何在序列化 Avro 消息时传递 Null 值
How to Pass Null value while serializing Avro message
我有定义为 Map<String, String> map;
的字段,我接受来自客户端的请求并将其发送到具有模式注册表中定义的模式的 Kafka 主题。
在架构中我将其定义为:
union{null, map<string>} map;
但是当有人向我发送一个空值时,我在将消息推送到 Kafka 主题时收到此错误:
Caused by: org.apache.kafka.common.errors.SerializationException: Error serializing Avro message
Caused by: java.lang.NullPointerException: null of string of map of union of event.AvroFlumeEvent
at org.apache.avro.generic.GenericDatumWriter.npe(GenericDatumWriter.java:184)
at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:178)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:83)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:95)
at io.confluent.kafka.serializers.KafkaAvroSerializer.serialize(KafkaAvroSerializer.java:53)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:841)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:803)
org.apache.flume.sink.DefaultSinkProcessor.process(DefaultSinkProcessor.java:67)
at org.apache.flume.SinkRunner$PollingRunner.run(SinkRunner.java:145)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
输入:
"dimensions": {
"countryCode": "US",
"Number": NULL
}
如何处理空值可以在模式本身中定义吗?
该架构表示 map values 不是联合,因此它们必须是非空字符串。地图本身可以为空,但
我不确定这在 IDL 中是否有效,但您可以尝试,假设您不希望地图可为空
map<union{null, string}>} map;
否则,您可以定义反序列化程序以将空字符串或某些默认值视为可为空的字段
我有定义为 Map<String, String> map;
的字段,我接受来自客户端的请求并将其发送到具有模式注册表中定义的模式的 Kafka 主题。
在架构中我将其定义为:
union{null, map<string>} map;
但是当有人向我发送一个空值时,我在将消息推送到 Kafka 主题时收到此错误:
Caused by: org.apache.kafka.common.errors.SerializationException: Error serializing Avro message
Caused by: java.lang.NullPointerException: null of string of map of union of event.AvroFlumeEvent
at org.apache.avro.generic.GenericDatumWriter.npe(GenericDatumWriter.java:184)
at org.apache.avro.generic.GenericDatumWriter.writeWithoutConversion(GenericDatumWriter.java:178)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:83)
at org.apache.avro.generic.GenericDatumWriter.write(GenericDatumWriter.java:73)
at io.confluent.kafka.serializers.AbstractKafkaAvroSerializer.serializeImpl(AbstractKafkaAvroSerializer.java:95)
at io.confluent.kafka.serializers.KafkaAvroSerializer.serialize(KafkaAvroSerializer.java:53)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:65)
at org.apache.kafka.common.serialization.ExtendedSerializer$Wrapper.serialize(ExtendedSerializer.java:55)
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:841)
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:803)
org.apache.flume.sink.DefaultSinkProcessor.process(DefaultSinkProcessor.java:67)
at org.apache.flume.SinkRunner$PollingRunner.run(SinkRunner.java:145)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
输入:
"dimensions": {
"countryCode": "US",
"Number": NULL
}
如何处理空值可以在模式本身中定义吗?
该架构表示 map values 不是联合,因此它们必须是非空字符串。地图本身可以为空,但
我不确定这在 IDL 中是否有效,但您可以尝试,假设您不希望地图可为空
map<union{null, string}>} map;
否则,您可以定义反序列化程序以将空字符串或某些默认值视为可为空的字段