如何在 Avro 模式中表示重复字段?

mkg*_*g90 2 schema serialization nested avro deserialization

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

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

ven*_*ata 5

您是否尝试过使用嵌套 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"] }
                ]
            }]
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

 /**
   * 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;
Run Code Online (Sandbox Code Playgroud)

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

  /**
   * 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;
Run Code Online (Sandbox Code Playgroud)

这是您要找的吗?