我们可以使用以下 Avro Schema 来定义 Map
{ "type": "map", "values": ["null", "int", "long", "float", "double", "boolean", "string", {"type": "array", "items": "int"}, {"type": "array", "items": "long"}, {"type": "array", "items": "float"}, {"type": "array", "items": "double"}, {"type": "array", "items": "boolean"}, {"type": "array", "items": "string"}, {"type": "record", "name": "MapValue", "fields": [{"name": "key", "type": "string"}, {"name": "value", "type": ["null", "int", "long", "float", "double", "boolean", "string", {"type": "array", "items": "int"}, {"type": "array", "items": "long"}, {"type": "array", "items": "float"}, {"type": "array", "items": "double"}, {"type": "array", "items": "boolean"}, {"type": "array", "items": "string"}, "MapValue"]}]}, "bytes"] }
该示例中的定义将会接受一个 Map,其中键为 String 类型,值类型可以是以下任意类型:
可以使用以下代码来创建 Map
import org.apache.avro.Schema; import org.apache.avro.generic.GenericData; import org.apache.avro.generic.GenericRecord;
Schema.Parser schemaParser = new Schema.Parser(); Schema schema = schemaParser.parse("{"type":"map","values":["null","int","long","float","double","boolean","string",{"type":"array","items":"int"},{"type":"array","items":"long"},{"type":"array","items":"float"},{"type":"array","items":"double"},{"type":"array","items":"boolean"},{"type":"array","items":"string"},{"type":"record","name":"MapValue","fields":[{"name":"key","type":"string"},{"name":"value","type":["null","int","long","float","double","boolean","string