avrospark-avroavsc

Avro - Add doc/description for each field


We are using avro for our schema definition. Is it possible to add field description for each of the fields in avro. I agree that we can add 'doc' at record level. we wanted to add description at a field level.


Solution

  • You can add doc at field level too.

    val str =
      """
        |{
        |  "type": "record",
        |  "name": "TestRecord",
        |  "namespace": "org.apache.avro.test",
        |  "doc": "test schema",
        |  "fields": [
        |    {
        |      "name": "name",
        |      "type": {
        |        "type": "string"
        |      },
        |      "doc": "this is name"
        |    },
        |    {
        |      "name": "age",
        |      "type": {
        |        "type": "long"
        |      },
        |      "doc": "this is age"
        |    }
        |  ]
        |}
        |""".stripMargin
    val schema = new Schema.Parser().parse(str)
    
    println(schema.getDoc)
    schema.getFields.forEach(field => println(field.doc()))
    

    output:

    test schema
    this is name
    this is age