How to define avro schema for complex json document?

前端 未结 2 562
梦如初夏
梦如初夏 2020-12-15 11:04

I have a JSON document that I would like to convert to Avro and need a schema to be specified for that purpose. Here is the JSON document for which I would like to define th

相关标签:
2条回答
  • 2020-12-15 11:27

    This online tool (http://avro4s-ui.landoop.com/) is very practical, you can generate the AVRO schema by a given valid json.

    0 讨论(0)
  • 2020-12-15 11:36

    You need to use Avro complex types, specifically arrays and records. And then nest these together:

    {
      "namespace" : "my.com.ns",
      "name": "myrecord",
      "type" :  "record",
      "fields" : [
         {"name": "uid", "type": "int"},
         {"name": "somefield", "type": "string"},
         {"name": "options", "type": {
            "type": "array",
            "items": {
                "type": "record",
                "name": "lvl2_record",
                "fields": [
                    {"name": "item1_lvl2", "type": "string"},
                    {"name": "item2_lvl2", "type": {
                        "type": "array",
                        "items": {
                            "type": "record",
                            "name": "lvl3_record",
                            "fields": [
                                {"name": "item1_lvl3", "type": "string"},
                                {"name": "item2_lvl3", "type": "string"}
                            ]
                        }
                    }}
                ]
            }
         }}
      ]
    }
    

    Also, to improve readiblity, you can split the schema into multiple files.

    0 讨论(0)
提交回复
热议问题