我想使用pyarrow从Rest服务器读取/查询拼花数据。目前我正在对数据进行分块、转换为熊猫、转储到json并流式传输块。像:
p = pq.ParquetDataset('/path/to/data.parquet', filters=filter, use_legacy_dataset=False)
batches = p._dataset.to_batches(filter=p._filter_expression)
(json.dumps(b.to_pandas().values.tolist()) for b in batches)
这实际上是与
ds = pq.ParquetDataset('/path/to/data.parquet',
use_legacy_dataset=False,
filters=filters)
df = ds.read().to_pandas()
data = pd.DataFrame(orjson.loads(orjson.dumps(df.values.tolist())))
没有网络操作系统。它比直接读给熊猫听慢50倍
df = ds.read().to_pandas()
有没有一种方法可以将parquet数据集序列化为一些二进制字符串,我可以通过超文本传输协议发送并在客户端解析?
您可以使用内存列式格式的箭头发送数据。它将比json高效和紧凑得多。但请记住,它将是二进制数据(与json不同,它不是人类可读的)
有关完整示例,请参阅文档。
在你的情况下,你想做这样的事情:
ds = pq.ParquetDataset('/path/to/data.parquet',
use_legacy_dataset=False,
filters=filters)
table = ds.read() # pa.Table
# Write the data:
batches = table.to_batches()
sink = pa.BufferOutputStream()
writer = pa.ipc.new_stream(sink, table.schema)
for batch in batches:
writer.write(batch)
writer.close()
buf = sink.getvalue()
# Read the data:
reader = pa.ipc.open_stream(buf)
read_batches = [b for b in reader]
read_table = pa.Table.from_batches(read_batches)
read_table.to_pandas()