python - 如何在 Windows 10 上使用 avro-python3 解析文件?
问题描述
我已经从 Microsoft Azure 下载了一个 AVRO 文件(带有 JSON 有效负载)到我的 Windows 10 计算机:
然后通过 pip 安装 python 3.8.5 和 avro 1.10.0,我尝试运行以下脚本:
import os, avro
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
reader = DataFileReader(open("48.avro", "rb"), DatumReader())
for d in reader:
print(d)
reader.close()
不幸的是,脚本没有打印任何内容。
然后我四处搜索并尝试添加如下模式:
schema_str = """
{
"type" : "record",
"name" : "EventData",
"namespace" : "Microsoft.ServiceBus.Messaging",
"fields" : [ {
"name" : "SequenceNumber",
"type" : "long"
}, {
"name" : "Offset",
"type" : "string"
}, {
"name" : "EnqueuedTimeUtc",
"type" : "string"
}, {
"name" : "SystemProperties",
"type" : {
"type" : "map",
"values" : [ "long", "double", "string", "bytes" ]
}
}, {
"name" : "Properties",
"type" : {
"type" : "map",
"values" : [ "long", "double", "string", "bytes", "null" ]
}
}, {
"name" : "Body",
"type" : [ "null", "bytes" ]
} ]
}
"""
schema = avro.schema.parse(schema_str)
reader = DataFileReader(open("48.avro", "rb"), DatumReader(schema, schema))
for d in reader:
print(d)
reader.close()
但这没有帮助,仍然没有打印任何内容。
虽然我期待会打印一个字典对象列表......
更新:
我在邮件列表中收到了不推荐使用 avro-python3 的回复。
我对原始 avro 的问题仍然存在,没有打印任何内容。
更新 2:
我必须道歉 - 我使用的 avro 文件不包含任何有用的数据。我感到困惑的原因是一位同事在为我测试时使用了同名的不同文件。
现在我已经在不同的 avro 文件上尝试了 avro 和 fastavro 模块,并且都可以工作。我也会看看 PySpark。
解决方案
正如 OneCricketeer 建议的那样,使用 PySpark 来读取 EventHub 生成的 avro 文件。在这里,PySpark:反序列化包含在 eventthub 捕获 avro 文件中的 Avro 序列化消息就是一个这样的例子。
推荐阅读
- r - 使用 dplyr 按条件估算缺失值
- python - 如何为标签塑造图像
- python-3.x - 使用运动结构进行 3D 重建
- wso2 - 将 wso2 API 和应用程序迁移到不同的环境
- amazon-web-services - 从超过 X 天的 S3 存储桶中删除文件夹、子文件夹和所有文件
- apache-spark - 将 Hive 存储与 Spark 集群(计算层)分离
- r - 如何使用 If-else?
- postman - 在 Postman 测试脚本中验证响应
- mysql - 使用 Prisma 执行 MySQL 查询耗时过长
- pdf - 我如何在java中使用apache poi和itext 7和pdf calligraph将docx转换为pdf?