【问题标题】:generating an AVRO schema from a JSON document从 JSON 文档生成 AVRO 模式
【发布时间】:2014-08-24 07:24:12
【问题描述】:

是否有任何工具能够从“典型”JSON 文档创建 AVRO 架构。

例如:

{
"records":[{"name":"X1","age":2},{"name":"X2","age":4}]
}

我发现http://jsonschema.net/reboot/#/ 生成一个'json-schema'

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "id": "http://jsonschema.net#",
  "type": "object",
  "required": false,
  "properties": {
    "records": {
      "id": "#records",
      "type": "array",
      "required": false,
      "items": {
        "id": "#1",
        "type": "object",
        "required": false,
        "properties": {
          "name": {
            "id": "#name",
            "type": "string",
            "required": false
          },
          "age": {
            "id": "#age",
            "type": "integer",
            "required": false
          }
        }
      }
    }
  }
}

但我想要一个 AVRO 版本。

【问题讨论】:

  • 你得到答案了吗?如果不是,那么您是否从 json 手动创建了 avro 模式? :|
  • 我也是.. 祝你好运!在我看来这是一项手动任务,我需要在自动化脚本中为定期生成的 JSON 数据文件生成 avro 模式文件:(

标签: json schema generator avro


【解决方案1】:

您可以使用 Apache Spark 和 python 轻松实现这一目标。首先从http://spark.apache.org/downloads.html 下载spark 分发包,然后使用pip 为python 安装avro 包。然后使用 avro 包运行 pyspark:

./bin/pyspark --packages com.databricks:spark-avro_2.11:3.1.0

并使用以下代码(假设 input.json 文件包含一个或多个 json 文档,每个文档位于单独的行中):

import os, avro.datafile

spark.read.json('input.json').coalesce(1).write.format("com.databricks.spark.avro").save("output.avro")
avrofile = filter(lambda file: file.startswith('part-r-00000'), os.listdir('output.avro'))[0]

with open('output.avro/' + avrofile) as avrofile:
    reader = avro.datafile.DataFileReader(avrofile, avro.io.DatumReader())
    print(reader.datum_reader.writers_schema)

例如:对于具有内容的输入文件:

{'string': 'somestring', 'number': 3.14, 'structure': {'integer': 13}}
{'string': 'somestring2', 'structure': {'integer': 14}}

脚本将导致:

{"fields": [{"type": ["double", "null"], "name": "number"}, {"type": ["string", "null"], "name": "string"}, {"type": [{"type": "record", "namespace": "", "name": "structure", "fields": [{"type": ["long", "null"], "name": "integer"}]}, "null"], "name": "structure"}], "type": "record", "name": "topLevelRecord"}

【讨论】:

    【解决方案2】:

    使用最新的 spark 3.1.2 和 python 3.9:

    ./bin/pyspark --packages org.apache.spark:spark-avro_2.12:3.1.2

    import os, avro.datafile
    spark.read.json('input.json').coalesce(1).write.format("avro").save("output.avro")
    avrofile = list(filter(lambda file: file.startswith('part-00000'), os.listdir('output.avro')))[0]`
    with open(avrofile.name,'rb') as af:
       reader = avro.datafile.DataFileReader(af, avro.io.DatumReader())`
       print(reader.datum_reader.writers_schema)`
    

    【讨论】:

      【解决方案3】:

      试试这个网站从 json 生成 avro 模式:

      https://toolslick.com/generation/metadata/avro-schema-from-json

      【讨论】:

        猜你喜欢
        • 2019-07-19
        • 1970-01-01
        • 2022-12-05
        • 1970-01-01
        • 2016-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-17
        相关资源
        最近更新 更多