Search code examples
javarecursionschemaavro

Avro schema object - recursion


Is this possible to make class in avro schema that have one of his parameter as themself?

Example in java:

public class Example {

private Integer value;
private Example example;

}

Solution

  • Avro schema is not defined in java, but in a json file usually with .avsc file extension. Here's an example of a recursive avro schema that represents a tree:

    {
      "type": "record",
      "name": "Node",
      "fields": [
        {
          "name": "value",
          "type": "long"
        },
        {
          "name": "children",
          "type": { "type": "array", "items": "Node" }
        }
      ]
    }
    

    So yes, it is perfectly possible to create recursive schemas.

    See also this issue, where even a shorter schema is defined:

    {
      "type": "record",
      "name": "RecursiveRecord",
      "fields": [{"name": "child", "type": "RecursiveRecord"}]
    }