Serialization 为什么不使用avro默认值?(使用avro-python)
我正在用Avro(使用python库)序列化一些数据,我很难弄清楚如何使“default”值起作用 我有这个模式:Serialization 为什么不使用avro默认值?(使用avro-python),serialization,avro,Serialization,Avro,我正在用Avro(使用python库)序列化一些数据,我很难弄清楚如何使“default”值起作用 我有这个模式: { "type": "record", "fields":[ {"name": "amount", "type": "long"}, {"name": "currency", "type": "string", "default": "EUR"} ], "name": "Monetary", } 因此,据我所知,我可
{
"type": "record",
"fields":[
{"name": "amount", "type": "long"},
{"name": "currency", "type": "string", "default": "EUR"}
],
"name": "Monetary",
}
因此,据我所知,我可以传递一个金额而不传递货币,货币字段将采用“EUR”值。但是,如果在编写时没有传递“currency”字段,则会出现错误avro.io.AvroTypeException:datum{…}不是模式xxx的示例
如果将货币字段的类型替换为union[“string”,“null”]
,则数据将序列化,但货币为null
因此,似乎根本没有考虑“默认”值
我错过了什么?默认值是否适用于基元类型
提前感谢以下是 当您尝试读取使用一个架构编写的实例并将其转换为使用另一个架构编写的实例时,将使用“default value”字段。如果第一个模式中不存在该字段(因此实例缺少该字段),那么您得到的实例将采用第二个模式的默认值 不是这样 使用相同的架构读/写实例时,“默认值”为未使用 因此,在您的示例中,当您将currency字段设置为默认值时,如果您尝试读取一个使用旧模式编写的实例,该模式不包含currency字段,那么您得到的实例将包含您在模式中定义的默认值 值得一提的是,当您使用union时,默认值仅指union的第一种类型
default: A default value for this field, used when reading instances that lack this field (optional)