tagomoris / presto-client-node

Distributed query engine Presto client library for node.js
MIT License
126 stars 57 forks source link

presto-client-node

Distributed query engine "Presto" 's client library for node.js.

var presto = require('presto-client');
var client = new presto.Client({user: 'myname'});

client.execute({
  query:   'SELECT count(*) as cnt FROM tblname WHERE ...',
  catalog: 'hive',
  schema:  'default',
  source:  'nodejs-client',
  state:   function(error, query_id, stats){ console.log({message:"status changed", id:query_id, stats:stats}); },
  columns: function(error, data){ console.log({resultColumns: data}); },
  data:    function(error, data, columns, stats){ console.log(data); },
  success: function(error, stats){},
  error:   function(error){}
});

Installation

npm install -g presto-client

Or add presto-client to your own package.json, and do npm install.

API

new Client(opts)

Instanciate client object and set default configurations.

return value: client instance object

execute(opts)

This is an API to execute queries. (Using "/v1/statement" HTTP RPC.)

Execute query on Presto cluster, and fetch results.

Attributes of opts [object] are:

Callbacks order (success query) is: columns -> data (-> data xN) -> success (or callback)

query(query_id, callback)

Get query current status. (Same with 'Raw' of Presto Web in browser.)

kill(query_id, callback)

Stop query immediately.

nodes(opts, callback)

Get node list of presto cluster and return it.

BIGINT value handling

Javascript standard JSON module cannot handle BIGINT values correctly by precision problems.

JSON.parse('{"bigint":1139779449103133602}').bigint //=> 1139779449103133600

If your query puts numeric values in its results and precision is important for that query, you can swap JSON parser with any modules which has parse method.

var JSONbig = require('json-bigint');
JSONbig.parse('{"bigint":1139779449103133602}').bigint.toString() //=> "1139779449103133602"
// set client option
var client = new presto.Client({
  // ...
  jsonParser: JSONbig,
  // ...
});

Development

When working on this library, you can use the included docker-compose.yml file to spin up a Presto and Trino DBs, which can be done with:

docker compose up

Once you see the following messages, you'll be able connect to Presto at http://localhost:18080 and Trino at http://localhost:18081, without username/password:

presto-client-node-trino-1   | 2023-06-02T08:12:37.760Z INFO    main    io.trino.server.Server  ======== SERVER STARTED ========
presto-client-node-presto-1  | 2023-06-02T08:13:29.760Z INFO    main    com.facebook.presto.server.PrestoServer ======== SERVER STARTED ========

After making a change, you can run the available test suite by doing:

npm run test

Versions

Todo

Author & License