This article will explain how to use Node.js bindings for REST API.
Beta Product
This module is still under active development. Some REST API bindings have not been implemented yet.
- Basic knowledge of Treasure Data, including the Toolbelt.
The module is published in NPM. Add the following line to your packages.json
file.
"td": ">=0.2.7"
The example below prints out all your tables to the console.
var TD = require('td');
var client = new TD('TREASURE_DATA_API_KEY');
client.listDatabases(function(err, results) {
var i;
var fnPrint = function(err, results) {
console.log(results);
};
if (err || !results.databases) {
return;
}
for (i = 0; i < results.databases.length; i++) {
client.listTables(results.databases[i].name, fnPrint);
}
});
The example below issues a query from Node.js. After issuing the query, a job_id
field is printed out. This can be used to query the results.
var TD = require('td');
var client = new TD('TREASURE_DATA_API_KEY');
client.hiveQuery('testdb', "SELECT code, COUNT(1) AS cnt FROM www_access GROUP BY code", function(err, results) {
console.log(results);
});
The example below lists and gets the status of jobs.
var TD = require('td');
var client = new TD('TREASURE_DATA_API_KEY');
var fnPrint = function(err, results) {
console.log(results);
};
// list recent 20 jobs
client.listJobs(fnPrint);
// recent 127 jobs of specific status
client.listJobs(0, 127, 'running', fnPrint);
client.listJobs(0, 127, 'success', fnPrint);
client.listJobs(0, 127, 'error', fnPrint);
client.listJobs(0, 127, 'error', fnPrint);
// get job status
client.showJob(job_id, fnPrint);
// get job result
client.jobResult(job_id, fnPrint);
For more details, refer to Treasure Data REST API Client.
- The source code is on GitHub.
- The Hive Query Engine