【问题标题】:How to use avro-python3 on Windows 10 to parse files?如何在 Windows 10 上使用 avro-python3 解析文件?
【发布时间】:2020-09-29 18:15:48
【问题描述】:

我已将 AVRO 文件(带有 JSON 有效负载)从 Microsoft Azure 下载到我的 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()

但这没有帮助,仍然没有打印任何内容。

虽然我期待会打印字典对象列表...

更新:

我在mailing list 上收到了 avro-python3 已被弃用的回复。

我的原始 avro 问题仍然存在,没有打印任何内容。

更新 2:

我必须道歉 - 我使用的 avro 文件不包含任何有用的数据。我感到困惑的原因是一位同事在为我测试时使用了同名的不同文件。

现在我已经在不同的 avro 文件上尝试了 avro 和 fastavro 模块,并且都可以正常工作。我也会看看 PySpark。

【问题讨论】:

  • fwiw,fastavro 应该会更好
  • 你也可以使用 PySpark 直接从 Azure / WASB 读取

标签: python python-3.x avro


【解决方案1】:

正如 OneCricketeer 建议的那样,使用 PySpark 来读取 EventHub 生成的 avro 文件。这里,PySpark: Deserializing an Avro serialized message contained in an eventhub capture avro file 就是这样一个例子。

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 1970-01-01
    • 2019-11-07
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多