gpt4 book ai didi

Avro 模式演变

转载 作者:行者123 更新时间:2023-12-04 01:04:15 25 4
gpt4 key购买 nike

我有两个问题:

  • 是否可以使用相同的读取器并解析用两个兼容的模式编写的记录,例如Schema V2Schema V1 相比只有一个额外的可选字段我想让读者理解两者?我认为这里的答案是否定的,但如果是,我该怎么做?
  • 我试过用 Schema V1 写记录并用 Schema V2 阅读它,但我收到以下错误:

    org.apache.avro.AvroTypeException: 发现 foo,期待 foo

  • 我使用了 avro-1.7.3 并且:
       writer = new GenericDatumWriter<GenericData.Record>(SchemaV1);
    reader = new GenericDatumReader<GenericData.Record>(SchemaV2, SchemaV1);

    以下是两个模式的示例(我也尝试添加命名空间,但没有成功)。

    架构 V1:
    {
    "name": "foo",
    "type": "record",
    "fields": [{
    "name": "products",
    "type": {
    "type": "array",
    "items": {
    "name": "product",
    "type": "record",
    "fields": [{
    "name": "a1",
    "type": "string"
    }, {
    "name": "a2",
    "type": {"type": "fixed", "name": "a3", "size": 1}
    }, {
    "name": "a4",
    "type": "int"
    }, {
    "name": "a5",
    "type": "int"
    }]
    }
    }
    }]
    }

    架构 V2:
    {
    "name": "foo",
    "type": "record",
    "fields": [{
    "name": "products",
    "type": {
    "type": "array",
    "items": {
    "name": "product",
    "type": "record",
    "fields": [{
    "name": "a1",
    "type": "string"
    }, {
    "name": "a2",
    "type": {"type": "fixed", "name": "a3", "size": 1}
    }, {
    "name": "a4",
    "type": "int"
    }, {
    "name": "a5",
    "type": "int"
    }]
    }
    }
    },
    {
    "name": "purchases",
    "type": ["null",{
    "type": "array",
    "items": {
    "name": "purchase",
    "type": "record",
    "fields": [{
    "name": "a1",
    "type": "int"
    }, {
    "name": "a2",
    "type": "int"
    }]
    }
    }]
    }]
    }

    提前致谢。

    最佳答案

    我遇到了同样的问题。这可能是 avro 的一个错误,但您可能可以通过将“default”:null 添加到“purchase”字段来解决。

    详情请查看我的博客:http://ben-tech.blogspot.com/2013/05/avro-schema-evolution.html

    关于Avro 模式演变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15350509/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com