Java 基于我的JSON文档设计一个Avro模式
最近我读了很多关于ApacheAvro的文章,我更倾向于使用它而不是使用JSON。目前,我们正在做的是,我们使用Java 基于我的JSON文档设计一个Avro模式,java,json,jackson,avro,Java,Json,Jackson,Avro,最近我读了很多关于ApacheAvro的文章,我更倾向于使用它而不是使用JSON。目前,我们正在做的是,我们使用Jackson序列化JSON文档,然后为每个行键/用户id将该序列化JSON文档写入Cassandra 然后我们有一个REST服务,它使用row键读取整个JSON文档,然后反序列化并进一步使用它 现在,在网上阅读时,它看起来像是,Avro需要一个模式。。。我不知道如何在ApacheAvro中为我的JSON文档提供一个模式 下面是我的JSON文档,在使用Jackson将其序列化后,我正在
Jackson
序列化JSON
文档,然后为每个行键/用户id
将该序列化JSON
文档写入Cassandra
然后我们有一个REST服务,它使用row键读取整个JSON
文档,然后反序列化并进一步使用它
现在,在网上阅读时,它看起来像是,Avro
需要一个模式。。。我不知道如何在ApacheAvro
中为我的JSON文档提供一个模式
下面是我的JSON
文档,在使用Jackson将其序列化后,我正在将其写入Cassandra。现在,如何为下面的JSON提供一个Avro
模式
{
"lv" : [ {
"v" : {
"site-id" : 0,
"categories" : {
"321" : {
"price_score" : "0.2",
"confidence_score" : "0.5"
},
"123" : {
"price_score" : "0.4",
"confidence_score" : "0.2"
}
},
"price-score" : 0.5,
"confidence-score" : 0.2
}
} ],
"lmd" : 1379231624261
}
有人能提供一个简单的例子吗?如何在我上面的JSON文档的基础上,在Avro中提出一个模式?感谢您的帮助。如上所述,定义avro模式的最简单方法是从他们所称的IDL开始。IDL是一种比Avro模式(json)更高级的语言,它使编写Avro模式更加简单 请参见此处的avro IDL: 要在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;
}
}
使用您喜欢的任何语言,您现在都可以生成一组对象,默认的“toString”操作是以JSON格式输出,如上所述。然而,Avro的真正威力来自它的压缩能力。你应该用avro二进制格式写出来,看看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" : {
}
}