Using Ruby Bindings for REST API

This article will explain how to use Ruby bindings for REST API.

Table of Contents


  • Basic knowledge of Treasure Data, including the toolbelt.
  • A table with some data. An example is provided in the Getting Started guide.
  • Basic knowledge of our query language.
  • Ruby 1.9 or newer


The Ruby bindings are included in the td (stands for ‘T’reasure ‘D’ata) gem. Please add the following line to your Gemfile.

gem 'td', "~> 0.10.22"

The source code is available at github.

List Databases and Tables

The example below lists the databases and tables. The API key is your authentication key. Please refer here to retrieve your API key.

require 'td'
require 'td-client'
cln.databases.each { |db|
  db.tables.each { |tbl|
    p tbl.db_name
    p tbl.table_name
    p tbl.count

Issue Queries

The example below issues a query from a Ruby program. The query API is asynchronous — you can check for query completion by polling the job periodically (e.g. by issuing job.finished? calls).

require 'td'
require 'td-client'
job = cln.query('testdb', 'SELECT COUNT(1) FROM www_access')
until job.finished?
  sleep 2
job.update_status!  # get latest info
job.result_each { |row| p row }
job.result_each(&block) does not put the job result into memory. It iterates through the rows in a streaming fashion.

List and Get the Status of Jobs

The example below lists and gets the status of jobs.

require 'td'
require 'td-client'

# recent 20 jobs

# recent 127 jobs of specific status, 127, 'running'), 127, 'success'), 127, 'error'), 127, 'killed')

# get job status
cln.job job_id

# get job result
cln.job_result job_id

Last modified: Mar 08 2015 06:40:46 UTC

If this article is incorrect or outdated, or omits critical information, please let us know. For all other issues, please see our support channels.