【问题标题】:Can you put comments in Avro JSON schema files?您可以在 Avro JSON 模式文件中添加注释吗?
【发布时间】:2013-05-23 01:39:04
【问题描述】:

我正在编写我的第一个 Avro 架构,它使用 JSON 作为架构语言。我知道您不能将 cmets 放入纯 JSON,但我想知道 Avro 工具是否允许 cmets。例如。也许它会在解析 JSON 之前剥离它们(如预处理器)。

编辑:我正在使用 C++ Avro 工具链

【问题讨论】:

    标签: json avro


    【解决方案1】:

    是的,但它是有限的。在架构中,Avro 数据类型“记录”、“枚举”和“固定”允许包含任意文档字符串的“文档”字段。例如:

    {"type": "record", "name": "test.Weather",
     "doc": "A weather reading.",
     "fields": [
         {"name": "station", "type": "string", "order": "ignore"},
         {"name": "time", "type": "long"},
         {"name": "temp", "type": "int"}
     ]
    }
    

    来自官方 Avro 规范:

    doc:一个 JSON 字符串,为该模式的用户提供文档(可选)。

    https://avro.apache.org/docs/current/spec.html#schema_record

    一个例子: https://github.com/apache/avro/blob/33d495840c896b693b7f37b5ec786ac1acacd3b4/share/test/schemas/weather.avsc#L2

    【讨论】:

      【解决方案2】:

      是的,您可以在 Avro JSON 模式中使用 C cmets:/* something */ or // something
      Avro 工具在解析期间会忽略这些表达式。
      编辑:它只适用于 Java API。

      【讨论】:

      • 这对我不起作用。如果我将任一样式的 cmets(/**/ 或 //)放入 JSON 文件,我会收到错误消息:“无法解析或编译架构:json 2f 中的意外字符”。我尝试将两种样式的 cmets 放在文件中的几个位置。
      • 我建议您在没有 cmets 的情况下编译过您的架构并且它可以工作。检查以下内容:最新版本的 avro-tools,确保您的模式编译(使用 avro 工具或Json validator)。供您参考,cmets 适用于我所有的 avro 架构...
      • 我使用的是最新版本的 1.7.4。我认为问题可能在于我们现在使用的是 C++ 版本,而您使用的是 Java 版本。很抱歉在我的问题中遗漏了这个细节。
      • 确实...我刚刚尝试了 C++ 版本,但它不起作用。感谢您强调这个 avro 问题。
      【解决方案3】:

      根据the current (1.9.2) Avro specification,允许将未定义的额外属性作为元数据放入:

      这允许您像这样添加 cmets:

      {
        "type": "record", 
        "name": "test",
        "comment": "This is a comment",
        "//": "This is also a comment",
        "TODO": "As per this comment we should remember to fix this schema" ,
        "fields" : [
          {
            "name": "a", "type": "long"
          },
          {
            "name": "b", "type": "string"
          }
        ]
      }
      

      【讨论】:

        【解决方案4】:

        不,它不能在 C++ 和 C# 版本中(从 1.7.5 开始)。如果您查看代码,他们只是将 JSON 推入 JSON 解析器而没有任何注释预处理 - 奇怪的编程风格。文档和语言支持似乎很草率...

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-02-27
          • 1970-01-01
          • 2022-06-23
          • 2011-09-02
          • 2011-04-15
          • 2019-07-19
          • 2010-09-19
          相关资源
          最近更新 更多