Код: Выделить всё
from fastapi import FastAPI, Response
import pyarrow as pa
import pyarrow.ipc as ipc
app = FastAPI()
@app.get("/api/getdata")
async def getdata():
table = pa.Table.from_pydict({
"name": ["Alice", "Bob", "Charlie"],
"age": [25, 30, 22]})
### Not really sure what goes here
## something like this...
sink = io.BytesIO()
with ipc.new_file(sink, table.schema) as writer:
for batch in table.to_batches():
writer.write(batch)
sink.seek(0)
return StreamingResponse(content=sink, media_type="application/vnd.apache.arrow.file")
Моя цель — иметь возможность получать данные на стороне js вот так...
Код: Выделить всё
import { tableFromIPC } from "apache-arrow";
const table = await tableFromIPC(fetch("/api/getdata"));
console.table([...table]);
Я попробовал сделать небольшими партиями, но это все равно не работало так:
Код: Выделить всё
@app.get("/")
def get_table():
table_or_ds = ds.dataset(file_or_files, format=format)
def gen_out():
with BytesIO() as sink:
with ipc.new_file(sink, table_or_ds.schema) as writer:
for batch in table_or_ds.to_batches():
writer.write_batch(batch)
yield sink.getvalue()
sink.seek(0)
sink.truncate()
yield sink.getvalue()
return StreamingResponse(gen_out(), media_type="application/vnd.apache.arrow.file")
Подробнее здесь: https://stackoverflow.com/questions/767 ... rrow-for-f