簡體   English   中英

在 avro 模式 java 中定義一個 object 數組

[英]Define An object Array In avro schema java

我有以下 avro 架構內容:

"fields":[
{
 "name" : "data",
 "type" :[
   "null" ,
   {
  "type":"array",
  "items":[
   "string",
   "null"
  ]
}
],
"default":null
}

因此,此數據當前是一個字符串列表。 所以,我們有一個新的需求:

We have set object of Type, com.test.Pos and com.test.Trans (basically both data of type position/transaction model has to get go to consumer as part of this Data field. So, that json parsing is easy at their結尾]

**If the model was only one type, I could see we can set it as below:

"type": {"type" :"array" ,"items":"com.test.Position"}}**

但是因為,我必須將 model com.test.Pos/com.test.Trans 設置為同一數據字段的一部分,[對於這兩個數據,我們將為消費者使用相同的 avsc 文件]

我們怎樣才能實現它? 我們應該如何定義 avro,以便數據字段也可以接受這兩種類型的 object(com.test.Pos 和 com.test.Trans)

您可以嘗試如下:

[
{   
  "type": "record",   
  "name": "com.test.Pos",   
  "aliases": ["com.test.Position"],   
  "fields" : [     
    {"name": "value", "type": "long"}
  ] 
},
{   
  "type": "record",   
  "name": "com.test.Trans",   
  "aliases": ["com.test.Transition"],   
  "fields" : [     
    {"name": "value", "type": "int"}
  ] 
},
{   
  "type": "record",   
  "name": "data",   
  "aliases": ["com.test.Union"],   
  "fields" : [     
    {"name": "foo", "type": ["null", "string", "com.test.Trans", "com.test.Pos"]}
  ] 
}]

轉換為:

{
  "oneOf" : [ {
    "$ref" : "#/definitions/record:com.test.Pos"
  }, {
    "$ref" : "#/definitions/record:com.test.Trans"
  }, {
    "$ref" : "#/definitions/record:data"
  } ],
  "definitions" : {
    "record:com.test.Pos" : {
      "type" : "object",
      "required" : [ "value" ],
      "additionalProperties" : false,
      "properties" : {
        "value" : {
          "type" : "integer",
          "minimum" : -9223372036854775808,
          "maximum" : 9223372036854775807
        }
      }
    },
    "record:com.test.Trans" : {
      "type" : "object",
      "required" : [ "value" ],
      "additionalProperties" : false,
      "properties" : {
        "value" : {
          "type" : "integer",
          "minimum" : -2147483648,
          "maximum" : 2147483647
        }
      }
    },
    "record:data" : {
      "type" : "object",
      "required" : [ "foo" ],
      "additionalProperties" : false,
      "properties" : {
        "foo" : {
          "oneOf" : [ {
            "type" : "null"
          }, {
            "type" : "string"
          }, {
            "$ref" : "#/definitions/record:com.test.Trans"
          }, {
            "$ref" : "#/definitions/record:com.test.Pos"
          } ]
        }
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM