有一种创建 Avro 模式的方法:
public void schemaCreator(String recordName, EntityModel entityModel){
SchemaBuilder.FieldAssembler<Schema> fields;
SchemaBuilder.RecordBuilder<Schema> record = SchemaBuilder.record(recordName);
fields = record.namespace("Test").fields();
for (AttributeModel attributeModel : entityModel.getAttributeModels()) {
fields = fields.name(attributeModel.getName()).type().nullable().stringType()
.noDefault();
}
}
结果,我们有一个图表:
{
"type":"record",
"name":"InfoData",
"namespace":"Test",
"fields":[
{
"name":"Setting",
"type":["string","null"]
},
{
"name":"Value",
"type":["string","null"]}]
}
如何为一个字段添加多种类型?例如:"type":["string","long","boolean","null"]}]
这是一个解决方案,因此您可以在循环中形成类型列表: