【问题标题】:Iterate pyarrow._flight.FlightStreamReader迭代 pyarrow._flight.FlightStreamReader
【发布时间】:2021-06-07 20:45:08
【问题描述】:

我如何遍历 reader,假设它是一个 pyarrow._flight.FlightStreamReader 对象。 可以从

获得
reader = client.do_get(flight_info.endpoints[0].ticket, options)

整个example.py 脚本来自https://github.com/dremio-hub/arrow-flight-client-examples/blob/main/python/example.py

目前我尝试使用reader.read_pandas(),以便它为整个 Dremio 结果生成一个数据框。不幸的是,如果查询有超过 5000 万行左右,它可能不适合数据框/或者可能没有足够的内存来存储它,我的进程就会被杀死。如何遍历阅读器对象并获取块,以便我可以为每个块生成数据帧。

当我使用时

for chunk in reader.read_chunk():
    print(chunk.to_pandas())

对于第一个块,它将仅从结果中转换/提取 3968 行并将其放入数据框中,但对于第二个块,它是一个 None 对象。我的示例确实有数百万行。

简而言之,如何按指定的块大小遍历阅读器?是否可以在不将其转换为数据帧的情况下按行打印这些块?

【问题讨论】:

    标签: python pyarrow dremio


    【解决方案1】:

    我写了以下内容,效果很好

    while True:
     try:
      batch, buf = reader.read_chunk()
      yield batch
     except StopIteration:
      break
    

    执行 batch.to_pandas() 的 cunsumer 函数

    缺少的部分是如何配置块大小

    【讨论】:

      猜你喜欢
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2020-09-13
      • 2016-02-17
      • 2021-08-24
      • 2016-10-22
      • 2019-06-04
      相关资源
      最近更新 更多