javascript - 如何在 javascript 中创建 ORC(或 parquet)文件?
问题描述
在服务器端 javascript 模块中,我需要将数据写入 ORC 文件,但找不到有关如何执行此操作的任何线索。理想情况下,我的模块也应该能够写入 parquet 文件。对于 ORC 案例,我也会对如何在 python 中做到这一点感兴趣。
对于镶木地板的情况,我已经看到这是通过使用Apache-Arrow 的 pyarrow 库在 python 中完成的。Apache Arrow 文档声称支持 ORC 和 parquet 文件格式。还有一个Apache Arrow 节点模块,但在他们的API 参考中,我在 ORC 或 parquet 上找不到任何东西。
在这里,我找到了对parquetjs和node-parquet节点模块的引用,但对 ORC 没有。另外,如果可能的话,我更喜欢使用 Apache 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 库。Quart和Hypercorn之类的框架非常棒,但它是如此的前沿,当你遇到麻烦时很难在网上找到答案 [/rant]。
推荐阅读
- java - 将视口宽度设置为滚动窗格宽度
- node.js - 如何将数组作为节点属性存储在 Neo4j 数据库中?
- javascript - 使用 Webpack 预编译所有 Handlebars 模板?
- dbeaver - 如何导出 DBeaver 连接配置?
- docusignapi - DocuSign Rest API v2.1 OpenAPI 规范招摇文件
- c# - 使用反射将对象转换为自定义类型列表
- azure-devops - 是否可以将 Azure 管道连接到本地服务器?
- kubernetes - 我的部署不知何故消失了,是否有任何地方跟踪“消失/删除”操作?
- c++ - 将 lambda 传递给 STL 集时出现“错误的函数调用”错误
- kotlin - 是否可以模拟延期延期
> 在科特林?