# Avro反序列化失败：使用新模式读取旧记录时出现'字段类型意外'

- **ID:** `data/avro-schema-field-type-mismatch`
- **领域:** data
- **类别:** schema_error
- **错误码:** `AvroTypeException`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

Avro模式演化更改修改了字段类型（例如从'int'改为'long'或从'string'改为'bytes'），没有适当的向后兼容性，导致旧记录反序列化失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Apache Avro 1.11+ | active | — | — |
| Confluent Schema Registry 7.5+ | active | — | — |
| Kafka Avro Deserializer 7.6+ | active | — | — |

## 解决方案

1. ```
   创建一个使用联合类型以接受新旧两种类型的新模式版本：将`"type": "long"`改为`"type": ["int", "long"]`。将其注册为新版本并更新读取器使用它。
   ```
2. ```
   使用自定义Avro datum读取器覆盖类型解析逻辑。对于Java：扩展`SpecificDatumReader`并重写`resolveType`以处理旧类型。
   ```
3. ```
   通过转换器重新处理旧记录，使用旧模式的读取器和新模式的写入器将其转换为新模式。使用Apache Avro工具的示例：`java -jar avro-tools-1.11.1.jar fromjson --schema-file old_schema.avsc old_data.json | java -jar avro-tools-1.11.1.jar tojson --schema-file new_schema.avsc > new_data.json`
   ```

## 无效尝试

- **** — This is a destructive operation that causes data loss and is often not feasible in production. (95% 失败率)
- **** — This does not fix the existing deserialization failure; old records still have the old type and cannot be read with the new schema. (85% 失败率)
- **** — This allows incompatible changes but does not help deserialize existing data; the error occurs at read time, not registration time. (70% 失败率)
