【问题标题】:How to represent repeated fields in Avro schema?如何表示 Avro 模式中的重复字段?
【发布时间】:2015-07-04 20:51:20
【问题描述】:

我的数据模型很少有固定字段和一组可变字段。变量字段作为一个块,在同一条记录中可以重复 o 到 n 次。

可以用对象人来比喻这一点。该名称在每条记录中只有一个条目,但他可以有 o 到 n 个地址,并且字段地址也有一个结构。有没有办法遍历地址模式以获取该人拥有的任意数量的地址?如何在 Avro 架构文件中提及这一点?

【问题讨论】:

    标签: serialization schema deserialization nested avro


    【解决方案1】:

    您是否尝试过使用嵌套的 Avro 架构。那应该可以解决您的一人多地址要求。这是一个有用的模式。

    {
        "type": "record",
        "name" : "person",
        "namespace" : "com.testavro",
        "fields": [
            { "name" : "personname", "type": ["null","string"] },
            { "name" : "personId", "type": ["null","string"] },
            {  "name" : "Addresses", "type": {
                "type": "array",
                "items": [  {
                  "type" : "record",
                  "name" : "Address",
                  "fields" : [
                    { "name" : "addressLine1", "type": ["null", "string"] }, 
                    { "name" : "addressLine2", "type": ["null", "string"] }, 
                    { "name" : "city", "type": ["null", "string"] }, 
                    { "name" : "state", "type": ["null", "string"] }, 
                    { "name" : "zipcode", "type": ["null", "string"] }
                    ]
                }]
                }
            }
        ]
    }
    

    当使用上述 avro 模式生成代码时,您将获得 person 类和 Address 类。 person 类的自动生成的类(仅字段声明)看起来像

     /**
       * RecordBuilder for person instances.
       */
      public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<person>
        implements org.apache.avro.data.RecordBuilder<person> {
    
        private java.lang.String personname;
        private java.lang.String personId;
        private java.util.List<java.lang.Object> Addresses;
    

    地址类(仅字段声明)看起来像

      /**
       * RecordBuilder for Address instances.
       */
      public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<Address>
        implements org.apache.avro.data.RecordBuilder<Address> {
    
        private java.lang.String addressLine1;
        private java.lang.String addressLine2;
        private java.lang.String city;
        private java.lang.String state;
        private java.lang.String zipcode;
    

    这就是你要找的吗?

    【讨论】:

      猜你喜欢
      • 2015-09-25
      • 2015-06-23
      • 2022-12-06
      • 2023-03-31
      • 1970-01-01
      • 2022-01-02
      • 2019-10-05
      • 1970-01-01
      • 2019-09-03
      相关资源
      最近更新 更多