假設你的Avro的模式看起來這樣:
{
"type" : "record",
"namespace" : "document",
"name" : "document_details",
"fields" : [
{ "name" : "documentName" , "type" : "string" },
{"name" : "documentChange" ,
"type" : ["null",
{"type" : "enum",
"namespace" : "document",
"name" : "documentChangeType",
"symbols" :["create","update","delete"]
}]
}
]
}
您可以在代碼中創建記錄此架構爲:
GenericRecord documentDetailsRecord = new GenericData.Record(schema);
GenericEnumSymbol enumSymbol = new GenericData.EnumSymbol(schema.getField("documentChange").schema().getTypes().get(1), "create");
e2.put("documentName", "someDocumentName");
e2.put("documentChange",enumSymbol);
你可以得到的架構的列表的所有字段工會如下:
schema.getField(<unionFieldName>).schema().getTypes()