使用@lakehouse-rs/flight-sql-client nodejs api 快速访问dremio 服务

@lakehouse-rs/flight-sql-client 是基于rust 开发的node arrow flight sql client ,dremio 目前也是推荐基于arrow flight sql 的访问模式

参考代码

  • package.json
{
  "name": "node-arrow-flight-sql",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "license": "MIT",
  "dependencies": {
    "@lakehouse-rs/flight-sql-client": "^0.0.10",
    "apache-arrow": "^15.0.2"
  }
}
  • app.js

自己部署一个dremio 服务,可以基于docker 进行快速启动

import { createFlightSqlClient } from '@lakehouse-rs/flight-sql-client';
import { tableFromIPC } from 'apache-arrow';
 
const options = {
  username: 'xxxxx',
  password: 'xxxxx',
  tls: false,
  host: '127.0.0.1',
  port: 32010,
  headers: [],
};
 
const client = await createFlightSqlClient(options);
 
const buffer = await client.query('select * from pg.public.sensor');
const table = tableFromIPC(buffer);
 
table.toArray().forEach((row) => {
  console.log(JSON.stringify(row));
})
 
const bufferv2 = await client.getTables({ includeSchema: false });
const tablev2  = tableFromIPC(bufferv2);
 
tablev2.toArray().forEach((row) => {
  console.log(JSON.stringify(row));
})
  • 效果

说明

基于flight-sql的好处很明显,速度比较快,我以前写过基于odbc+arrow flight 模式的,直接基于arrow flight sql 是一种更加方便的模式
对于基于数据开发的应用flight-sql 是一个高性能的协议值得使用,。nodejs 的实现是基于了napi 这个使用rust 开发node addon 扩展的
框架,后边说明下内部处理原理,返回类似rest api response 格式的处理

const client = await createFlightSqlClient(options);
const buffer = await client.query(sql);
const table = tableFromIPC(buffer);
let res = {
  rowCount: table.numRows,
  schema: table.schema.fields.map((f) => {
    return { name: f.name, type: { name: f.metadata.get("ARROW:FLIGHT:SQL:TYPE_NAME") } };
  }),
  rows: table.toArray(),
};

参考资料

https://www.npmjs.com/package/apache-arrow
https://github.com/roeap/flight-sql-client-node
https://napi.rs/

posted on   荣锋亮  阅读(13)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-05-01 juicefs 方便mac 进行utm 虚拟机的文件共享
2023-05-01 通过sshfs 方便mac 进行utm 虚拟机的文件共享
2023-05-01 bytehound 参考试用
2023-05-01 bytehound centos 7构建说明
2023-05-01 bytehound linux 内存profiler工具
2022-05-01 openresty 提供的几个新的生命周期阶段指令
2022-05-01 openresty lua 文件处理的一些技巧

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示