【问题标题】:Using in-memory filesystem in `pyarrow` tests在 `pyarrow` 测试中使用内存文件系统
【发布时间】:2019-10-15 04:34:51
【问题描述】:

我有一些pyarrow Parquet 数据集编写代码。我想要一个集成测试来确保文件被正确写入。我想通过将一个小的示例数据块写入内存文件系统来做到这一点。但是,我正在努力为 Python 找到一个与pyarrow 兼容的内存文件系统接口。

您会在下面找到包含filesystem 变量的sn-p 代码。我想用内存中的文件系统替换 filesystem 变量,以后可以在集成测试中以编程方式对其进行检查。

import pyarrow.parquet as pq
pq.write_to_dataset(
        score_table,
        root_path=AWS_ZEBRA_OUTPUT_S3_PREFIX,
        filesystem=filesystem,
        partition_cols=[
            EQF_SNAPSHOT_YEAR_PARTITION,
            EQF_SNAPSHOT_MONTH_PARTITION,
            EQF_SNAPSHOT_DAY_PARTITION,
            ZEBRA_COMPUTATION_TIMESTAMP
        ]
    )

【问题讨论】:

  • 也许临时文件可以提供帮助 - 但最终仍会保存在磁盘上:docs.python.org/3/library/tempfile.html
  • 谢谢@Jay。我想避免为集成测试创建临时文件,因为这会引入外部系统依赖性并增加测试不稳定的风险。

标签: python filesystems parquet pyarrow


【解决方案1】:

如果filesystemNone,您可以将内存中的文件对象传递给write_to_dataset

所以你的电话可能变成:

from io import BytesIO
import pyarrow.parquet as pq

with BytesIO() as f:
    pq.write_to_dataset(
        score_table,
        root_path=f,
        filesystem=None,
        partition_cols=[
            EQF_SNAPSHOT_YEAR_PARTITION,
            EQF_SNAPSHOT_MONTH_PARTITION,
            EQF_SNAPSHOT_DAY_PARTITION,
            ZEBRA_COMPUTATION_TIMESTAMP
        ]
    )

pyarrow 源码中的相关行:

def resolve_filesystem_and_path(where, filesystem=None):
    """
    Return filesystem from path which could be an HDFS URI, a local URI,
    or a plain filesystem path.
    """
    if not _is_path_like(where):
        if filesystem is not None:
            raise ValueError("filesystem passed but where is file-like, so"
                             " there is nothing to open with filesystem.")
        return filesystem, where

https://github.com/apache/arrow/blob/207b3507be82e92ebf29ec7d6d3b0bb86091c09a/python/pyarrow/filesystem.py#L402-L411

【讨论】:

  • 谢谢!我会试试这个 - 它看起来很有希望!一旦我尝试过,我会更新你。
  • 杰伊,这似乎不起作用。 pyarrow 抱怨 None 对象没有 filesystem 所期望的属性。你用哪个pyarrow 版本测试过这个?
  • 如果您遇到问题,能否请您打开 Apache Arrow 的 JIRA 问题?
【解决方案2】:

最后,我手动实现了pyarrow.FileSystem ABC 的一个实例。似乎将mock 用于测试目的失败了,因为pyarrow(不是以最Pythonic 的方式)检查传递给write_to_dataset:https://github.com/apache/arrow/blob/5e201fed061f2a95e66889fa527ae8ef547e9618/python/pyarrow/filesystem.py#L383filesystem 参数的类型。我建议更改此方法中的逻辑以不显式检查类型(甚至isinstance 更可取!)以便于测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 2014-03-01
    • 2020-09-14
    • 2022-09-23
    • 1970-01-01
    相关资源
    最近更新 更多