我想使用Apache Arrow将数据从Django后端发送到Angular前端。我想使用数据帧/表的字典作为消息中的有效负载。 pyarrow可能以这种方式在python微服务之间共享数据,但是我找不到用arrow的javascript实现的方法。
在JavaScript端,是否有一种方法可以反序列化/序列化以字符串为键,而数据框/表为值的字典,使用Arrow?
答案 0 :(得分:0)
是的,可以使用pyarrow和ArrowJS中的RecordBatchReader和RecordBatchWriter IPC原语来实现此目的。
在python端,您可以像这样将Table序列化为缓冲区:
import pyarrow as pa
def serialize_table(table):
sink = pa.BufferOutputStream()
writer = pa.RecordBatchStreamWriter(sink, table.schema)
writer.write_table(table)
writer.close()
return sink.getvalue().to_pybytes()
# ...later, in your route handler:
bytes = serialize_table(create_your_arrow_table())
然后,您可以在响应正文中发送字节。如果有多个表,则可以将每个表中的缓冲区串联为一个大有效负载。
我不确定存在什么功能来用python编写多部分/表单主体响应,但是如果您希望将表及其名称(或您希望的任何其他元数据)一起发送,那可能是制作响应的最佳方法包括在内。)
在JavaScript方面,您可以使用Table.from()
(如果只有一个表)或RecordBatchReader
(如果您有多个表)或要读取的内容来读取响应每个RecordBatch以流方式:
import { Table, RecordBatchReader } from 'apache-arrow'
// easy if you want to read the first (or only) table in the response
const table = await Table.from(fetch('/table'))
// or for mutliple tables on the same stream, or to read in a streaming fashion:
for await (const reader of RecordBatchReader.readAll(fetch('/table'))) {
// Buffer all batches into a table
const table = await Table.from(reader)
// Or process each batch as it's downloaded
for await (const batch of reader) {
}
}
您可以在这里的ArrowJS测试中看到更多示例: https://github.com/apache/arrow/blob/3eb07b7ed173e2ecf41d689b0780dd103df63a00/js/test/unit/ipc/writer/stream-writer-tests.ts#L40
中消费和产生Arrow负载而编写的fastify插件中看到一些示例。