我有一个用 Scala 编写的服务,它通过基于 Avro 模式的 Kafka 主题接收对象。原始架构如下所示:
{
"type" : "record",
"name" : "SomeClass",
"fields" : [ {
"name" : "component",
"type" : "string"
}, {
"name" : "priority",
"type" : [ "null", "int" ]
}, {
"name" : "time",
"type" : {
"type" : "long",
"logicalType" : "timestamp-micros"
}
}, {
"name" : "anotherRecord",
"type" : {
"type" : "array",
"items" : {
"type" : "record",
"name" : "result",
"fields" : [a complex structure]
}
}
}, {
"name" : "trace",
"type" : "string"
}]
}
我通过添加两个默认值为null
. 新架构如下所示:
{
"type" : "record",
"name" : "SomeClass",
"fields" : [ {
"name" : "component",
"type" : "string"
}, {
"name" : "priority",
"type" : [ "null", "int" ]
}, {
"name" : "time",
"type" : {
"type" : "long",
"logicalType" : "timestamp-micros"
}
}, {
"name" : "anotherRecord",
"type" : {
"type" : "array",
"items" : {
"type" : "record",
"name" : "result",
"fields" : [a complex structure]
}
}
}, {
"name" : "trace",
"type" : "string"
}, {
"name" : "configs",
"type" : ["null", {
"type" : "map",
"values" : "string"
}],
"default": null
}, {
"name" : "notification",
"type" : ["null", { a complex structure }],
"default": null
}]
}
新生成的案例类如下所示:
final case class SomeClass(component: String, priority: Option[Int], time: Long, anotherRecord: Seq[result], trace: String, configs: Option[Map[String, String]] = None, notification: Option[someNotification] = None)
我可以看到 2 个新参数定义为默认值None
.
但是,当我使用新模式运行服务并尝试使用旧模式读取其他服务发送的消息时(其中没有新参数),我收到反序列化错误。
我在这里想念什么?难道我做错了什么?我的 avro 架构有问题吗?
任何帮助,将不胜感激。谢谢你。