Search code examples
hadoopschemaavrokiteavsc

Creating Avro Schema without Class(-Object)


At the moment i'm serializing a class to an avsc schema. This works pretty good:

DatasetDescriptor desc = new DatasetDescriptor.Builder().schema(ClassName.class).build();

But, I need to create an AVSC without any given class dynamically. Is this possible?


Solution

  • It is possible. You can create a schema on the fly or read an avro schema. The following code use hardcoded schema but is basically the same idea

    Schema schema = new Schema.Parser().parse("{\"namespace\": \"example.avro\",\n" +
            " \"type\": \"record\",\n" +
            " \"name\": \"User\",\n" +
            " \"fields\": [\n" +
            "     {\"name\": \"name\", \"type\": \"string\"},\n" +
            "     {\"name\": \"favorite_number\",  \"type\": [\"int\", \"null\"]},\n" +
            "     {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n" +
            " ]\n" +
            "}");
    DatasetDescriptor desc = new DatasetDescriptor.Builder().schema(schema).build();