如何正确地将avro架构转换为json架构

时间:2016-10-12 00:25:07

标签: java json avro jackson-dataformat-avro

我有以下json数据对象:

{
    "name": "John",
    "favorite_number": 5,
    "favorite_color" : "green"
}

此对象的JSON架构如下所示:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Person",
    "description": "some description",
    "type": "object",
    "properties": {
        "name": {
            "description": "name",
            "type": "string"
        },
        "favorite_number": {
            "type": "number",
        },
        "favorite_color": {
            "type": "string",
        },
    },
    "required": ["name", "favorite_number","favorite_color"]
}

我可以使用这个JSON模式来验证我的数据对象是否符合它:

public static boolean isJsonValid(String schemaText, String jsonText) throws ProcessingException, IOException
    {   
        final JsonSchema schemaNode = getSchemaNode(schemaText);
        final JsonNode jsonNode = getJsonNode(jsonText);
        return isJsonValid(schemaNode, jsonNode);
    } 

在我的java应用程序中,我从REST API调用接收到此对象的相应AVRO架构,该架构如下所示:

{
 "namespace": "example.avro",
 "type": "record",
 "name": "Person",
 "fields": [
     {"name": "name", "type": "string"},
     {"name": "favorite_number",  "type": ["int", "null"]},
     {"name": "favorite_color", "type": ["string", "null"]}
 ]
}

是否有一种通常可接受的方式将此类AVRO架构转换为JSON架构?

1 个答案:

答案 0 :(得分:0)

  1. 下载:avro-tools-1.7.4.jar(或最新版本from repository
  2. 运行:java -jar avro-tools-1.7.4.jar tojson avro-filename.avro>output-filename.json
  3. 这将create output-filename.json文件包含所有数据。如果output-filename.json已经存在,则会覆盖它。