You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从JSON生成Avro文件失败:转换后无法被IntelliJ识别

JSON转Avro后无法被IntelliJ查看的原因及解决

问题场景

我有两个文件:

  • twitter.json:
{
    "username": "test"
}
  • twitter.avsc:
{
    "name": "MyClass",
    "type": "record",
    "namespace": "com.acme.avro",
    "fields": [
      {
        "name": "username",
        "type": "string"
      }
    ]
  }

执行命令java -jar avro-tools-1.10.0.jar fromjson --schema-file twitter.avsc twitter.json > twitter.avro完成转换后,用IntelliJ的parquet/avro查看器打开twitter.avro,却提示该文件不是有效的Avro文件。

原因分析

问题出在shell重定向符号>:

  • 在Windows的cmd/PowerShell环境中,>会默认将输出编码为UTF-16或带BOM的UTF-8格式,而Avro文件要求纯二进制存储,这种编码转换会直接破坏Avro的二进制结构。
  • 即便在Linux/macOS的shell环境中,若终端存在特殊编码配置,也可能导致输出文件的二进制结构受损。

解决方法

放弃使用shell重定向,改用avro-tools自带的--output参数指定输出文件,让工具直接以二进制格式写入内容:

java -jar avro-tools-1.10.0.jar fromjson --schema-file twitter.avsc twitter.json --output twitter.avro

内容的提问来源于stack exchange,提问作者Omega

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 21:25:19