【发布时间】:2017-09-10 18:24:48
【问题描述】:
我正在使用 Apache Kafka 向 Kafka 主题发送消息。我正在尝试在 Avro 模式中使用联合,包括用于消息验证的枚举类型。但是我在联合中使用枚举类型时遇到了问题。我正在通过 POSTMAN 工具使用 Kafka REST API 将记录/消息发布到具有模式验证的主题。下面是请求负载,包括内联模式和记录 -
{
"key_schema": "{\"type\": \"record\", \"name\": \"key\", \"fields\": [{\"name\": \"keyInput\", \"type\": \"string\"}]}",
"value_schema": "{\"type\": \"record\", \"name\": \"value\", \"fields\": [{\"name\": \"valueInput1\", \"type\": \"string\"},{\"name\": \"valueInput2\",\"type\":[{\"type\":\"enum\",\"name\":\"actorobjType\",\"symbols\":[\"Agent\",\"Group\"]},\"null\"],\"default\":null}]}",
"records": [
{
"key": {
"keyInput": "testUser-key"
},
"value": {
"valueInput1": "testUser-value",
"valueInput2": "Agent"
}
}
]
}
当我尝试使用上述请求有效负载插入记录时出现以下错误 -
{
"error_code": 42203,
"message": "Conversion of JSON to Avro failed: Failed to convert JSON to Avro: Expected start-union. Got VALUE_STRING"
}
在包括stackoverflow在内的不同网站上搜索后,我找到了suggestion
要求在传递记录时明确指定类型,如下所示 -
{
"key_schema": "{\"type\": \"record\", \"name\": \"key\", \"fields\": [{\"name\": \"keyInput\", \"type\": \"string\"}]}",
"value_schema": "{\"type\": \"record\", \"name\": \"value\", \"fields\": [{\"name\": \"valueInput1\", \"type\": \"string\"},{\"name\": \"valueInput2\",\"type\":[{\"type\":\"enum\",\"name\":\"actorobjType\",\"symbols\":[\"Agent\",\"Group\"]},\"null\"],\"default\":null}]}",
"records": [
{
"key": {
"keyInput": "testUser-key"
},
"value": {
"valueInput1": "testUser-value",
"valueInput2": {
"enum": "Agent"
}
}
}
]
}
然后我面临以下错误 -
{
"error_code": 42203,
"message": "Conversion of JSON to Avro failed: Failed to convert JSON to Avro: Unknown union branch enum"
}
同样的建议适用于其他类型的联合,如字符串和映射,但对于包括枚举在内的联合,这似乎不起作用。
我还认为可能有一些其他类型需要用于枚举规范,因此我尝试了一些其他词,如下所示 -
"valueInput2": {
"string": "Agent"
}
和
"valueInput2": {
"enumeration": "Agent"
}
但它们似乎都不起作用。请帮我解决这个问题。
【问题讨论】:
标签: enums apache-kafka union avro