将多个不同记录的数组写入 Avro 格式,写入同一个文件

Writing an array of multiple different Records to Avro format, into the same file

我们有一些遗留文件格式,我需要将其迁移到 Avro 存储。棘手的部分是记录基本上有

全部存储在同一个文件中,没有任何顺序,完全混杂在一起。 (这是 旧版...)

在Java/object-oriented编程中,可以将我们的记录概念表示如下:

abstract class RecordWithCommonFields {
   private Long commonField1;
   private String commonField2;
   ...
}

class RecordTypeA extends RecordWithCommonFields {
   private Integer specificToA1;
   private String specificToA1;
   ...
}

class RecordTypeB extends RecordWithCommonFields {
   private Boolean specificToB1;
   private String specificToB1;
   ...
}

想象一下数据是这样的:

commonField1Value;commonField2Value,TYPE_IS_A,specificToA1Value,specificToA1Value
commonField1Value;commonField2Value,TYPE_IS_B,specificToB1Value,specificToB1Value

所以我想处理传入的文件并将其内容写入 Avro 格式,以某种方式代表不同类型的记录。

有人可以给我一些关于如何实现这一点的想法吗?

来自 Avro 用户电子邮件列表的

Nandor 非常友好地帮助我解决 this answer,感谢他;这个答案是为了记录在案,以防其他人遇到同样的问题。

他的解决方案很简单,基本上是使用组合而不是继承,通过引入一个公共容器class和一个引用特定子class.

的字段

使用这种方法,映射如下所示:

{
  "namespace": "com.foobar",
  "name": "UnionRecords",
  "type": "array",
  "items": {
    "type": "record",
    "name": "RecordWithCommonFields",
    "fields": [
      {"name": "commonField1", "type": "string"},
      {"name": "commonField2", "type": "string"},
      {"name": "subtype", "type": [
        {
          "type" : "record",
          "name": "RecordTypeA",
          "fields" : [
            {"name": "integerSpecificToA1", "type": ["null", "long"] },
            {"name": "stringSpecificToA1", "type": ["null", "string"]}
          ]
        },
        {
          "type" : "record",
          "name": "RecordTypeB",
          "fields" : [
            {"name": "booleanSpecificToB1", "type": ["null", "boolean"]},
            {"name": "stringSpecificToB1", "type": ["null", "string"]}
          ]
        }
      ]}
    ]
  }
}