Avro - Add doc/description for each field

3.1k Views Asked by At

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.

1

There are 1 best solutions below

1
On BEST ANSWER

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