如上所述,定义 avro 模式的最简单方法是从他们所谓的 IDL 开始。IDL 是一种比 Avro 模式 (json) 更高级的语言,并且使编写 avro 模式更加直接。
在此处查看 avro IDL:http: //avro.apache.org/docs/current/idl.html
要在 JSON 中定义上面的内容,您将在 IDL 中定义一组记录,如下所示:
@namespace("com.sample")
protocol sample {
record Category {
union {null, string} price_score = null;
union {null, string} confidence_score = null;
}
record vObject {
int site_id = 0;
union {null, map<Category>} categories = null;
union {null, float} price_score = null;
union {null, float} confidence_score = null;
}
record SampleObject {
union {null, array<vObject>} lv = null;
long lmd = -1;
}
}
当您运行编译器工具(如上面该网站上列出的)时,您将获得一个生成的 avro 模式,如下所示:
{
"protocol" : "sample",
"namespace" : "com.sample",
"types" : [ {
"type" : "record",
"name" : "Category",
"fields" : [ {
"name" : "price_score",
"type" : [ "null", "string" ],
"default" : null
}, {
"name" : "confidence_score",
"type" : [ "null", "string" ],
"default" : null
} ]
}, {
"type" : "record",
"name" : "vObject",
"fields" : [ {
"name" : "site_id",
"type" : "int",
"default" : 0
}, {
"name" : "categories",
"type" : [ "null", {
"type" : "map",
"values" : "Category"
} ],
"default" : null
}, {
"name" : "price_score",
"type" : [ "null", "float" ],
"default" : null
}, {
"name" : "confidence_score",
"type" : [ "null", "float" ],
"default" : null
} ]
}, {
"type" : "record",
"name" : "SampleObject",
"fields" : [ {
"name" : "lv",
"type" : [ "null", {
"type" : "array",
"items" : "vObject"
} ],
"default" : null
}, {
"name" : "lmd",
"type" : "long",
"default" : -1
} ]
} ],
"messages" : {
}
}
使用您喜欢的任何语言,您现在可以生成一组对象,并且默认的“toString”操作是以 JSON 格式输出,如上所示。然而,Avro 的真正强大之处在于它的压缩能力。您应该真正以 avro 二进制格式写出,以了解 avro 的真正好处。
希望这可以帮助!