Last active
November 18, 2020 11:52
-
-
Save Schm1tz1/3229c6dc2e5bf1539da5e26840e11607 to your computer and use it in GitHub Desktop.
How to handle nested AVRO Schemas: Union, several Files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void nestedSchemaTest() throws IOException, NoSuchAlgorithmException { | |
Schema.Parser parser = new Schema.Parser(); | |
// nested schemas as union | |
parser.parse(new File("schemaUnion.avsc")); | |
// load dependent first, then "derived" schema | |
parser.parse(new File("schema1.avsc")); | |
parser.parse(new File("schema2.avsc")); | |
System.out.println(parser.getTypes()); | |
System.out.println(parser.getTypes().get("person").toString(true)); | |
System.out.println(parser.getTypes().get("person2").toString(true)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "record", | |
"name": "Address2", | |
"fields": [ | |
{ | |
"name": "streetaddress", | |
"type": "string" | |
}, | |
{ | |
"name": "city", | |
"type": "string" | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "record", | |
"name": "person2", | |
"fields": [ | |
{ | |
"name": "firstname", | |
"type": "string" | |
}, | |
{ | |
"name": "lastname", | |
"type": "string" | |
}, | |
{ | |
"name": "address", | |
"type": "Address2" | |
} | |
] | |
} | |
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"type": "record", | |
"name": "Address", | |
"fields": [ | |
{ | |
"name": "streetaddress", | |
"type": "string" | |
}, | |
{ | |
"name": "city", | |
"type": "string" | |
} | |
] | |
}, | |
{ | |
"type": "record", | |
"name": "person", | |
"fields": [ | |
{ | |
"name": "firstname", | |
"type": "string" | |
}, | |
{ | |
"name": "lastname", | |
"type": "string" | |
}, | |
{ | |
"name": "address", | |
"type": "Address" | |
} | |
] | |
} | |
] |
Is there any dynamic way to load the dependent first, then "derived" schema
At least I haven't found a "AVRO-native" way to do this. Some ideas:
- You can define everything in one place so you only have "one" schema with the substructure defined on first usage
- Wrap the parser with some exception-handling to get the unresolved dependencies, resolve and retry. Maybe quick and dirty, but too much random trial-and-error for me t.b.h.
- more clever solution: preload and parse the schemas with some king of JSON-parser, build a list/tree of the dependencies and generate a loading order for the schema files by traversing this tree
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is there any dynamic way to load the dependent first, then "derived" schema