This article describes how to import the data from your Ruby applications, using Treasure Data’s Ruby SDK.

Prerequisites

  • Basic knowledge of Ruby, Gems, and Bundler.

  • Basic knowledge of Treasure Data.

  • Ruby 1.9 or higher (for local testing).

Install ‘td’ Gem

Add the td (‘T’reasure ‘D’ata) gem to your Gemfile.

gem 'td', "~> 0.11.1"

Install the gem locally via bundler.

$ bundle install

Modify Your App

The ‘td’ gem comes with a built-in library for recording in-app events. Insert code, as shown in the following example, to record events from your app. Further details regarding the event logger API can be found on GitHub.

YOUR_API_KEY should be your actual apikey string. Retrieve your API key.

# Initialization
TreasureData::Logger.open('production',
                          :apikey=>"YOUR_API_KEY",
                          :auto_create_table=>true)

# Example1: login event
TD.event.post('login', {:uid=>123})

# Example2: follow event
TD.event.post('follow', {:uid=>123, :from=>'TreasureData', :to=>'kzk_mover'})

# Example3: pay event
TD.event.post('pay',
              {:uid=>123, :item_name=>'Stone of Jordan',
               :category=>'ring', :price=>100, :count=>1})

Your event-logging code should be placed near its corresponding event-generating code.

When using the td gem, the posted records are buffered in the memory locally at first, and the data is uploaded every 5 minutes. Because a dedicated thread uploads the data into the cloud, it doesn’t affect your application’s response time.

The local buffer also has a size limit. If the local data exceeds this limit, the records are uploaded immediately.

Confirming Data Import

The data gets uploaded every 5 minutes. You can confirm the data import from TD Console or CLI.

From TD Console

To confirm that your data has been uploaded successfully, check your dataset from the web browser.

From CLI

Issue the td tables command if you have a CLI client.

$ td tables
+------------+------------+------+-----------+
| Database   | Table      | Type | Count     |
+------------+------------+------+-----------+
| production | login      | log  | 1         |
| production | follow     | log  | 1         |
| production | pay        | log  | 1         |
+------------+------------+------+-----------+
  • No labels