avro 动态嵌套映射 - 类似于 jsonschema 对象

问题描述 投票:0回答:1

在 jsonschema 中,您可以像这样定义对象的通用属性:

{
  "description": "Records the raw request and response of an HTTP exchange for debugging purposes.",
  "type": "object",
  "definitions": {},
  "$schema": "http://json-schema.org/draft-04/schema#",
  "properties": {
    "response_json": {
      "description": "The response body as an optional JSON structure.",
      "type": "object",
      "patternProperties": {
        "^[a-zA-Z0-9_-]+$": { }
      },
      "additionalProperties": false
    }
  },
  "required": [],
  "additionalProperties": false
}

这基本上是说您可以拥有任何键(通过

patternProperties
)和任何类型的值
{ }

这类似于 avro 地图,但我不知道如何使地图具有嵌套地图来容纳这些有效负载。

{
  "response_json": {
    "foo": "bar",
    "baz": {
      "nested": "qux"
    }
  }
}
{
  "response_json": {
    "moo": "cow",
    "fish": {
      "pseudotropheus": {"type": "zebra"}
    }
  }
}

使用 avro 地图,您可以执行类似的操作,但我认为这不允许任意级别的嵌套。我不会只是继续硬编码并联合所有类型吗?

{
  "fields": [
    {
      "name": "response_json",
      "type": {
        "type": "map",
        "values": [
          "string",
          {
            "type": "map",
            "values": "string"
          }
        ]
      }
    }
  ],
  "name": "sample",
  "namespace": "foo",
  "type": "record"
}

之前看似相关但没有完全回答我的问题:

用于具有随机名称的非结构化数据的 Avro 架构 Avro 模式可以有未知/动态字段名称吗? https://issues.apache.org/jira/browse/AVRO-2993

我尝试了许多 avro 模式定义,但没有什么像 jsonschema 那样足够动态。

我希望有一个例子,或者确认这是不可能的,并且我至少必须知道形状。

avro jsonschema
1个回答
0
投票

您是否正在寻找递归映射类型,例如:

{
    "namespace": "io.nson.avro",
    "type": "record",
    "name": "Map",
    "fields": [
        {
            "name": "entries",
            "type": { "type": "map", "values": "Map" }
        }
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.