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

PCAP中AVRO编码数据读取报错AssertionError: -29求助

问题解决:读取裸AVRO编码数据时的AssertionError

问题原因

你用DataFileReader读取裸AVRO编码数据是错误的——这个类专门用于读取标准Avro数据文件(包含Obj\x01文件头、内置schema等容器结构),但你的数据是从TCP负载提取的纯Avro编码内容,没有Avro数据文件的容器头部,因此触发解析错误。

正确解决方案

改用DatumReader配合BinaryDecoder直接解码裸Avro二进制数据,代码调整如下:

import avro
from avro.io import DatumReader, BinaryDecoder
import io

# 加载AVRO schema
schema = avro.schema.parse(open("test.avsc").read())
reader = DatumReader(schema)

# 读取二进制文件并定位到AVRO数据起始位置
with open("test.bin", mode="rb") as f:
    f.seek(79)
    raw_bytes = f.read()

# 用BinaryDecoder解码单条Avro记录
buff = io.BytesIO(raw_bytes)
decoder = BinaryDecoder(buff)
data = reader.read(decoder)
print(data)

补充说明

  • 如果TCP负载包含多条连续的Avro记录,可以循环调用reader.read(decoder),直到抛出EOFError表示数据读取完毕。
  • 确认seek(79)确实定位到了Avro数据的起始字节——从你提供的十六进制数据看,该偏移位置正好对应预期字段1.0.0的起始,定位是正确的。

内容的提问来源于stack exchange,提问作者E.Bobuh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 07:57:47