[英]Defining optional record type in Avro schema
我在為以下 xml 定義 avro 架構時遇到困難,其中 SubElement1 是可選的:
XML 文件
<?xml version="1.0" encoding="UTF-8"?>
<Element1 attribute1="attr_value1" attribute2="attr_value2">
<SubElement1 attribute1="attr_value">
</Element1>
AVRO 架構
{
"namespace": "com.kafka.avro",
"type": "record",
"name": "Element1",
"fields": [
{"name": "attribute1", "type": "string"},
{"name": "attribute2", "type": "string"},
{"name": "SubElement1",
"type":["null", {
"type": "record",
"name": "SubElement1",
"fields":[
{"name" : "attribute1", "type" : "string" , "default":""}
]
}], "default":null
}
]
}
當 SubElement1 不存在時,這可以正常工作:
"SubElement1": null
當存在 SubElement1 時,這會導致添加命名空間的位置如下:
"SubElement1": {
"com.kafka.avro.SubElement1": {
"attribute1": "attr_value1"
}
}
我想完全省略命名空間部分,如下所示:
"SubElement1": {
"attribute1": "attr_value1"
}
這可能嗎?
看起來您的 output 是 JSON 編碼。 不幸的是,規范規定編碼應該是這樣的: https://avro.apache.org/docs/current/spec.html#json_encoding
Avro 問題跟蹤器上有一個關於此功能請求的未決問題: https://issues.apache.org/jira/browse/AVRO-1582 。 在那個問題上,我認為有人提供了一些可以做你想做的事情的代碼,但到目前為止,標准庫還沒有辦法做到這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.