用于将字段从原语演变为联合的 Avro 模式解析

ski*_*jon 6 java avro

我正在使用 Avro 1.7.0 使用其Java 的通用表示 API,但在处理我们当前的模式演变案例时遇到了问题。我们在这里处理的场景是通过将字段更改null为该原始类型的并集来使原始类型字段成为可选字段。

我将使用一个简单的例子。基本上,我们的模式是:

  • 初始:具有一个类型字段的记录 int
  • 第二个版本:相同的记录,相同的字段名称,但类型现在是nulland 的并集int

根据Avro 规范的模式解析章节,这种情况的解析应该是:

如果读者是一个联合,但作者不是
联合中第一个与作者模式匹配的读者联合模式是针对它递归解析的。如果不匹配,则发出错误信号。

我的解释是,我们应该正确解析用初始模式序列化的数据,因为这int是读者模式中联合的一部分。

但是,当运行使用版本 2 读回使用版本 1 序列化的记录的测试时,我得到

org.apache.avro.AvroTypeException: Attempt to process a int when a union was expected.

这是一个测试,正好显示了这一点:

@Test
public void testReadingUnionFromValueWrittenAsPrimitive() throws Exception {
    Schema writerSchema = new Schema.Parser().parse("{\n" +
            "    \"type\":\"record\",\n" +
            "    \"name\":\"NeighborComparisons\",\n" +
            "    \"fields\": [\n" +
            "      {\"name\": \"test\",\n" +
            "      \"type\": \"int\" }]} ");

    Schema readersSchema = new Schema.Parser().parse(" {\n" +
            "    \"type\":\"record\",\n" +
            "    \"name\":\"NeighborComparisons\",\n" +
            "    \"fields\": [ {\n" +
            "      \"name\": \"test\",\n" +
            "      \"type\": [\"null\", \"int\"],\n" +
            "      \"default\": null } ]  }");

    // Writing a record using the initial schema with the 
    // test field defined as an int
    GenericData.Record record = new GenericData.Record(writerSchema);
    record.put("test", Integer.valueOf(10));        
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    JsonEncoder jsonEncoder = EncoderFactory.get().
       jsonEncoder(writerSchema, output);
    GenericDatumWriter<GenericData.Record> writer = new 
       GenericDatumWriter<GenericData.Record>(writerSchema);
    writer.write(record, jsonEncoder);
    jsonEncoder.flush();
    output.flush();

    System.out.println(output.toString());

    // We try reading it back using the second schema 
    // version where the test field is defined as a union of null and int
    JsonDecoder jsonDecoder = DecoderFactory.get().
        jsonDecoder(readersSchema, output.toString());
    GenericDatumReader<GenericData.Record> reader =
            new GenericDatumReader<GenericData.Record>(writerSchema, 
                readersSchema);
    GenericData.Record read = reader.read(null, jsonDecoder);

    // We should be able to assert that the value is 10 but it
    // fails on reading the record before getting here
    assertEquals(10, read.get("test"));
}
Run Code Online (Sandbox Code Playgroud)

我想知道我的期望是否正确(这应该能成功解决吗?)或者我没有正确使用 avro 来处理这种情况。

ski*_*jon 6

将原始模式迁移到 null 和原始模式的联合的期望是正确的。

上面代码的问题在于解码器是如何创建的。解码器需要作者的模式而不是读者的模式。

而不是这样做:

JsonDecoder jsonDecoder = DecoderFactory.get().
    jsonDecoder(readersSchema, output.toString());
Run Code Online (Sandbox Code Playgroud)

应该是这样的:

JsonDecoder jsonDecoder = DecoderFactory.get().
    jsonDecoder(writerSchema, output.toString());
Run Code Online (Sandbox Code Playgroud)

感谢 Doug Cutting 在 avro 的用户邮件列表上的回答:http ://mail-archives.apache.org/mod_mbox/avro-user/201208.mbox/browser