首页 > 解决方案 > 如何在 javascript 中创建 ORC(或 parquet)文件?

问题描述

在服务器端 javascript 模块中,我需要将数据写入 ORC 文件,但找不到有关如何执行此操作的任何线索。理想情况下,我的模块也应该能够写入 parquet 文件。对于 ORC 案例,我也会对如何在 python 中做到这一点感兴趣。

对于镶木地板的情况,我已经看到这是通过使用Apache-Arrow 的 pyarrow 库在 python 中完成的。Apache Arrow 文档声称支持 ORC 和 parquet 文件格式。还有一个Apache Arrow 节点模块,但在他们的API 参考中,我在 ORC 或 parquet 上找不到任何东西。

在这里,我找到了对parquetjsnode-parquet节点模块的引用,但对 ORC 没有。另外,如果可能的话,我更喜欢使用 Apache Arrow。

有人对我有任何指示吗?

标签: javascriptpyarrowapache-arrow

解决方案


更新:

您的问题促使我整理了一些概念验证来做箭头-> parquetjs。不幸的是 parquetjs 有一个面向行的编写器,但是通过编写器管道表行迭代器似乎工作正常:

$ node index.js 
{ int: 0, str: 'foo' }
{ int: 1, str: 'bar' }
{ int: 2, str: 'baz' }

原答案:

我们不支持在 ArrowJS 中读取或写入 parquet。我不知道节点 parquet 实现的成熟度,所以我还没有探索 ArrowJS 和 ParquetJS 之间可能存在什么样的互操作。

到目前为止,我解决这个问题的方法是在必要时使用 pyarrow 编写 parquet 文件,通常在我们想要读取或写入长期存储的边界处。我意识到这只是一个解决方案,如果你能负担得起一些 python 服务。

如果不是(这是一个相对不常见的操作,或者您可以在 python 解释器启动时等待),您可以通过从节点生成 python 子进程并通过 pyarrow 管道表来使用动态语言获得一些快速的乐趣:

const duplexer = require('duplexer')
const { finished: eos } = require('stream')
const { spawn } = require('child_process')
const { RecordBatchWriter } = require('apache-arrow')

const writer = new RecordBatchWriter()
writer.writeAll(your_arrow_table()).close()
await eos(writer.pipe(to_parquet_file('out/file.parquet')))

function spawn_python_script(code) {
    const child = spawn('python', ['-c', code]);
    return duplexer(child.stdin, child.stdout);
}

function to_parquet_file(out_path) {
    return spawn_python_script(`
import sys
import pyarrow as pa
import pyarrow.parquet as pq

# read all the batches in from stdin
table = pa.RecordBatchStreamReader(sys.stdin.buffer).read_all()

# write table to the out_path
pq.write_table(table, '${out_path}')

sys.stdout.write('wrote table to \'${out_path}\')
sys.stdout.flush()
`)
}

如果将 python 脚本保存到文件并从中读取路径sys.argv[1],python 启动起来会更快一些(但仍然需要一两秒)。

我不熟悉 ORC 库,但我想他们的一个 Python API 中有某种 parquet <-> ORC 转换。我发现不幸的是,这些工具中的大多数都不存在于 JS 中,或者即使它们存在,它们也是新生/废弃的(这就是我们必须编写 ArrowJS 实现的原因)。

很遗憾,因为如今 node 在 i/o 方面相当不错,并且通过 python 实现相同的吞吐量需要大量挖掘最新的asyncio /ASGI 库。QuartHypercorn之类的框架非常棒,但它是如此的前沿,当你遇到麻烦时很难在网上找到答案 [/rant]。


推荐阅读