Node.js Client for TD-API

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.

Prerequisites

  • Basic knowledge of Treasure Data, including the Toolbelt .

Setup

The module is published in NPM. Add the following line to your packages.json file.

Copy
Copied
"td": ">=0.2.7"

Basic Use

List Databases and Tables

The example below prints out all your tables to the console.

Copy
Copied
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);
  } 
});

Issue Queries

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.

Copy
Copied
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);
});

List and Get the Status of Jobs

The example below lists and gets the status of jobs.

Copy
Copied
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);

API reference

For more details, refer to Treasure Data REST API Client.

Further Reading