如何在Avro消息中告诉何时需要命名空间?



由于几个问题,我很难在Avro中推广名称空间的使用。最近的一个问题是消息何时应该包含名称空间。我现在认为,使用名称空间会带来太多麻烦,因为会出现明显的不一致性。最近,我们一直在处理Confluent架构注册表中的以下错误:

"error_code": 42203,
"message": "Conversion of JSON to Object failed: Failed to convert JSON to Avro: Unknown union branch id"

此错误的来源似乎是正在发送的消息中缺少名称空间标识符。

例如,在我的模式中,我有一个名为Component的部分,其定义如下:
{
"name": "component",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "Component",
"fields": [
{
"name": "typeCode",
"type": {
"type": "enum",
"name": "ComponentTypeCode",
"symbols": [
"PremiumDue",
"LoanInterestDue"
]
}
},
{
"name": "messageFlag",
"type": "YesNo",
"doc": "Whether there is a message associated with this entry."
},
{
"name": "amountDue",
"type": "string",
"doc": "Amount of the specified component. For example 123.78"
}
]
}
}

}

可以在没有命名空间的情况下发送,没有问题:

{
"component": [
{
"typeCode": "AppliedDividend",
"messageFlag": "Yes",
"amountDue": "192775.34"
}
]
}

然而,在模式的另一部分,我有一个像这样定义的enum:

{
"name": "otherLetterCode",
"type": [
"null",
{
"type": "enum",
"name": "OtherLetterCode",
"symbols": [
"PaidUpDateModLetter",
"IndemnityLetter"
]
}
]

}

在消息中,它必须像这样发送:

"otherLetterCode": {
"my.namespace.OtherLetterCode": "IndemnityLetter"
}

这似乎不一致。如果整个模式是在名称空间中定义的,那么我们是否应该a)在消息中使用名称空间,或者b)根本不必在消息中使用名称空间?我希望component看起来像这样:

{
"my.namespace.component": [
{
"typeCode": "AppliedDividend",
"messageFlag": "Yes",
"amountDue": "192775.34"
}
]
}

继续使用名称空间有意义吗?如果有的话,我们如何确保我们的各种系统客户端能够创建在适当位置拼写出名称空间值的消息,假设它们不能使用自动代码生成实用程序?

区别在于otherLetterCode是两个可能值的并集。JSON的avro规范是大多数人期望的方式(即字段名是键名,字段值是键值),除了联合的情况。您可以查看此处的规范:https://avro.apache.org/docs/current/spec.html#json_encoding,但在联合的情况下,如果值是非空的,则期望包含命名空间类型。

专:

For example, the union schema ["null","string","Foo"], where Foo is a record name, would encode:
- null as null;
- the string "a" as {"string": "a"}; and
- a Foo instance as {"Foo": {...}}, where {...} indicates the JSON encoding of a Foo instance.

最新更新