如何本地读取羽毛/箭头文件?

2024-02-16

I have feather格式文件sales.feather我用来在之间交换数据python and R.

在 R 中我使用以下命令:

df = arrow::read_feather("sales.feather", as_data_frame=TRUE)

在 python 中我使用了:

df = pandas.read_feather("sales.feather")

将数据从该文件加载到内存中运行的 Spark 实例的最佳方法是什么pyspark? 我也想控制pyspark.StorageLevel用于从羽毛读取的数据。

我不想使用 pandas 加载数据,因为它对我的 19GB Feather 文件(从 45GB csv 创建)存在段错误。


丑陋的黑客 - 使用箭头地图 https://spark.apache.org/docs/latest/api/python/reference/pyspark.sql/api/pyspark.sql.DataFrame.mapInArrow.html.

import pyarrow as pa


def read_arrow(spark, filename, schema=None):

    def mapper(iterator):
        with pa.memory_map(filename, "rb") as source:
            f = pa.ipc.open_file(source)
            for batch in iterator:
                for i in batch['id']:
                    yield f.get_batch(i.as_py())

    tmp_reader = pa.ipc.open_file(filename)
    num_batches = tmp_reader.num_record_batches
    if schema is None:
        # read first batch and convert just one row to pandas
        tmp_row = tmp_reader.get_batch(0)[:1]
        schema = spark.createDataFrame(tmp_row.to_pandas()).schema
    return spark.range(num_batches).mapInArrow(mapper, schema)


df = read_arrow(spark, "some-data.arrow")
df.show()

(奖励:在 Spark 中使用零复制的内存映射值!雅虎!)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何本地读取羽毛/箭头文件? 的相关文章

随机推荐