我可以 [反] 序列化箭头/js 实现中的数据帧字典吗?

gab*_*mgp 5 javascript python ipc apache-arrow pyarrow

我想使用 Apache Arrow 将数据从 Django 后端发送到 Angular 前端。我想使用数据帧/表的字典作为消息中的有效负载。pyarrow 可以在 python 微服务之间以这种方式共享数据,但我找不到箭头的 javascript 实现的方法。

有没有办法反序列化/序列化一个字典,字符串作为键,数据帧/表作为箭头在javascript端的值?

pta*_*lor 3

是的,使用 pyarrow 和 ArrowJS 中的 RecordBatchReader 和 RecordBatchWriter IPC 原语可以实现这种变体。

在 python 方面,您可以将表序列化到缓冲区,如下所示:

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())
Run Code Online (Sandbox Code Playgroud)

然后您可以发送响应正文中的字节。如果您有多个表,则可以将每个表的缓冲区连接为一个大负载。

我不确定在 python 中存在什么功能来编写多部分/表单主体响应,但是如果您希望表及其名称(或您希望包含的任何其他元数据)发送,这可能是制作响应的最佳方法。

Table.from()在 JavaScript 方面,您可以使用(如果您只有一个表)读取响应,或者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) {
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以在我们的 ArrowJS 测试中看到更多示例: https: //github.com/apache/arrow/blob/3eb07b7ed173e2ecf41d689b0780dd103df63a00/js/test/unit/ipc/writer/stream-writer-tests.ts#L40

您还可以在我编写的一个小 fastify 插件中看到一些示例,该插件用于在节点中消费和生成 Arrow 有效负载: https: //github.com/trxcllnt/fastify-arrow