从 Node JS 调用 Presto SQL 查询

问题描述

我正在尝试通过 nodejs 调用 Presto Query,以使用 presto-stream-client (https://www.npmjs.com/package/presto-stream-client) 将结果取回 js 变量。 nodejs 在 aws lambda 中运行。我目前的测试代码如下。代码退出时出现错误,指出文件 /tmp/test.txt 不存在。所以我假设 client.execute(query) 未能生成 test.txt 数据文件。但是,查询执行部分没有记录错误。如果有人能在这里有所启发,那就太好了。

const fs = require('fs');
const util = require('util');
const pipeline = util.promisify(require('stream').pipeline);
const { Client } = require('presto-stream-client');

    //Presto query
    
    const client = new Client({
        user: 'presto',host: "presto-host-url",port: "8080",catalog: 'catalogname',schema:  'schemaname'
    }); 
    
(async ()=>{
    const statement = await client.execute({query:'SELECT mydata FROM catalog.schema.myprestotable',success: function(error,stats){  console.log(" error on success : " + error); },error:   function(error){ console.log(" error on failure : " + error); }
    });

    const writeStream = fs.createWriteStream('/tmp/test.txt');

    statement.on('state_change',(currentState,stats)=>{
        console.log(`state changed to ${currentState} for query ${statement.query_id}`);
        console.log(stats);
    });
    await pipeline(statement,writeStream);

})();

var content = fs.readFileSync('/tmp/test.txt','utf8');
console.log(content)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)