有一种方法可以在Avro Python中进行查找吗?

huangapple go评论50阅读模式
英文:

Is there a way to seek in an Avro Python?

问题

在编写多个Avro记录并通过DataFileWriter.sync存储它们的文件偏移量后,我想能够读取特定偏移量处的记录。

DataFileWriter.sync的文档中写道:

> 返回当前位置作为一个值,可以传递给DataFileReader.seek(long)

但这似乎是不正确的,因为DataFileReader没有名为seek的方法。我正在使用Avro Python库版本1.11.1。

英文:

After writing a number of Avro records and storing their file offsets via DataFileWriter.sync, I'd like to be able to read the record at a particular offset.

The documentation for DataFileWriter.sync states:

> Return the current position as a value that may be passed to
> DataFileReader.seek(long)

But this seems incorrect as DataFileReader has no method named seek. I am using the Avro Python library version 1.11.1.

答案1

得分: 0

以下是您要翻译的内容:

寻找底层文件对象的方法。

def read(path, offset):
    with DataFileReader(open(path, 'rb'), DatumReader()) as reader:
        reader.reader.seek(offset)
        result = next(reader)
        return result
英文:

Seeking the underlying file object works.

def read(path, offset):
    with DataFileReader(open(path, 'rb'), DatumReader()) as reader:
        reader.reader.seek(offset)
        result = next(reader)
        return result

huangapple
  • 本文由 发表于 2023年5月28日 12:20:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定