gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:15 24 4
gpt4 key购买 nike

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

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

最佳答案

是的,在 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())

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

我不确定在 python 中编写 multipart/form-b​​ody 响应有什么功能,但如果您希望表与它们的名称(或您希望的任何其他元数据)一起发送,这可能是制作响应的最佳方式包括)。

在 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 小插件中看到一些示例:https://github.com/trxcllnt/fastify-arrow

关于javascript - 我可以 [反] 序列化箭头/js 实现中的数据帧字典吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51409288/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com