Issue
Consider some POJOs such as follows.
class Configuration {
private String id;
private Map<String, LocationConfig> locationConfiguration;
}
class LocationConfig {
private String locationId;
private Integer capacity;
private Boolean available;
}
How can I come up with a JSON based Avro schema for the above? As far as I could find, even though maps are supported in Avro, the key type should always be String (which I can somehow live with) and the values cannot be of Record
type. Is there a way around this? The documentation has only follows.
Upon research I could find somewhat of a similar question, which doesn't look very useful.
Solution
Yes you can add complex POJO as Map's value.
For e.g. I had below schema initially
{
"type": "record",
"name": "click_event",
"namespace": "exercise2",
"fields": [
{"name": "email", "type": "string"},
{"name": "timestamp", "type": "string"},
{"name": "uri", "type": "string"},
{"name": "number", "type": "int"},
],
}
Then I wanted to add a complex type i.e. map which in turn contained POJO objects.
{
'about':
ClickAttribute(element='a', content='reinvent intuitive infrastructures'),
'index':
ClickAttribute(element='button', content='repurpose cross-media schemas')
}
I added it in this way
{
"type": "record",
"name": "click_event",
"namespace": "exercise2",
"fields": [
{"name": "email", "type": "string"},
{"name": "timestamp", "type": "string"},
{"name": "uri", "type": "string"},
{"name": "number", "type": "int"},
# Below complex type is added by me.
{
"name" : "attributes",
"type" : {
"type" : "map",
"values" : {
"name" : "click_attribute",
"type" : "record",
"fields" : [
{"name" : "element", "type" : "string"},
{"name" : "content", "type" : "string"}
]
}
}
}
],
}
Answered By - shubham
Answer Checked By - Gilberto Lyons (JavaFixing Admin)