如何在Avro架构中表示重复的字段?

时间:2015-07-02 21:47:38

标签: serialization schema deserialization nested avro

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

对象人可以用作此类比。该名称在每条记录中只有一个条目,但是他可以有n到n个地址,字段地址也有一个结构。有没有办法循环遍历该人拥有的任意数量地址的地址架构?我如何在Avro架构文件中提到这一点?

1 个答案:

答案 0 :(得分:4)

您是否尝试过使用嵌套的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;

和Address类(仅字段声明)看起来像

  /**
   * 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;

这是你在找什么?