验证大小与avro模式有关的json对象数组

时间:2019-12-16 06:37:56

标签: apache-nifi avro

我正在尝试使用apache Nifi中的avro模式验证其大小是动态的json对象数组。我知道,如果知道大小,我们可以编写一个模式进行验证。即使尺寸是动态的,也有一种方法可以验证。

Json条目如下所示

"multi_location" : [
        {
            "cityState" : "Seattle, Washington", 
            "country" : "United States", 
            "city" : "Seattle", 
            "location" : "Seattle, Washington, United States", 
            "cityCountry" : "Seattle, United States", 
            "cityStateCountry" : "Seattle, Washington, United States", 
            "state" : "Washington", 
            "mapQueryLocation" : "Seattle, Washington, United States", 
            "stateCountry" : "Washington, United States"
        }, 
        {
            "cityState" : "Toronto, Ontario", 
            "country" : "Canada", 
            "city" : "Toronto", 
            "location" : "Toronto, Ontario, Canada", 
            "cityCountry" : "Toronto, Canada", 
            "cityStateCountry" : "Toronto, Ontario, Canada", 
            "state" : "Ontario", 
            "mapQueryLocation" : "Toronto, Ontario, Canada", 
            "stateCountry" : "Ontario, Canada"
        }, 
        {
            "cityState" : "Vancouver, British Columbia", 
            "country" : "Canada", 
            "city" : "Vancouver", 
            "location" : "Vancouver, British Columbia, Canada", 
            "cityCountry" : "Vancouver, Canada", 
            "cityStateCountry" : "Vancouver, British Columbia, Canada", 
            "state" : "British Columbia", 
            "mapQueryLocation" : "Vancouver, British Columbia, Canada", 
            "stateCountry" : "British Columbia, Canada"
        }
    ]

1 个答案:

答案 0 :(得分:0)

只要数组中的每个对象具有相同的架构,就不需要知道数组的大小即可对其进行验证。在Avro模式中,您只需指定一个Array type,其中的项由上面列出的每个单独项的模式定义。如果可能未为数组中的某些对象填充某些字段,则可以通过将字段设置为Union并使其包含“空”类型来使它们成为“可选”。