【问题标题】:Design an Avro schema basis on my JSON document根据我的 JSON 文档设计 Avro 模式
【发布时间】:2013-09-15 08:01:09
【问题描述】:

这些天我读了很多关于Apache Avro 的文章,我更倾向于使用它而不是使用JSON。目前,我们正在做的是,我们正在使用Jackson 序列化JSON 文档,然后为每个row key/user id 将序列化JSON 文档写入Cassandra

然后我们有一个 REST 服务,它使用行键读取整个 JSON 文档,然后对其进行反序列化并进一步使用它。

现在在网络上阅读时,Avro 需要预先设置一个模式...我不确定如何在 Apache Avro 中为我的 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 中提出一个模式?感谢您的帮助。

【问题讨论】:

    标签: java json jackson avro


    【解决方案1】:

    如上所述,定义 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 的真正好处。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-08-24
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多