2016-05-19

Treasure DataのAPIを叩いてみた

Treasure DataのAPIを叩くときは各種言語のライブラリを利用するのがほとんどだと思いますが、REST APIを実際に叩いて何が出来るのか確認してみましたー

まずはcurlで叩いてみる

認証はAPI KeyをAuthorizationヘッダに入れるシンプルな方式。

ジョブの発行はこんな感じ

$ curl -i -X POST \
 -H "Content-Type:application/json" \
 -H "Authorization:TD1 {TREASURE_DATA_API_KEY}" \
 -d \
 '{
 "query": "{query}"
 } ' \
 'https://api.treasuredata.com/v3/job/issue/{type}/{database}'

レスポンス

{
  "job":"{job_id}",
  "database":"test_hoge",
  "job_id":"{job_id}"
}

 

ジョブのステータスを確認する

$ curl -i -X GET \
 -H "Authorization:TD1 {TREASURE_DATA_API_KEY}" \
 'https://api.treasuredata.com/v3/job/status/{job_id}'

レスポンス

{
  "status": "success",
  "cpu_time": null,
  "result_size": 51,
  "duration": 2,
  "job_id": "{job_id}",
  "created_at": "2016-05-15 14:58:25 UTC",
  "updated_at": "2016-05-15 14:58:27 UTC",
  "start_at": "2016-05-15 14:58:25 UTC",
  "end_at": "2016-05-15 14:58:27 UTC"
}

 

結果を取得する

$ curl -i -X GET \
 -H "Authorization:TD1 {TREASURE_DATA_API_KEY}" \
 'https://api.treasuredata.com/v3/job/result/{job_id}?format=csv'

レスポンス。CSVだとこんな感じ

NOT RT,true,526
RT,true,5209
RT,false,15934
NOT RT,false,5907

Apexで書いてみる

Apexを使うとこんな感じで書けます。リモートサイトの設定も忘れずに。
public with sharing class TDClient {
    private static final String TD_API_BASE_URL = 'https://api.treasuredata.com';
    
    private String tdKey;
    
    public TDClient(String tdKey) {
        this.tdKey = tdKey;
    }
    
    private String sendHttpRequest(String method, String path, String body) {
        HttpRequest req = new HttpRequest();
        req.setHeader('Authorization', 'TD1 ' + this.tdKey);
        req.setTimeout(60000);
        
        req.setEndpoint(TD_API_BASE_URL + path);
        req.setMethod(method);
        
        if (method == 'POST') {
            req.setHeader('Content-type', 'application/json');
            req.setHeader('Content-Length', String.valueOf(body.length()));
            req.setBody(body);
        }
        
        Http http = new Http();
        HTTPResponse res = http.send(req);
        return res.getBody();       
    }
    
    public Map<String, Object> issue(String type, String database, String table, String query) {
        String resBody = this.sendHttpRequest(
            'POST', 
            '/v3/job/issue/{type}/{database}'.replace('{type}', type)
                                             .replace('{database}', database), 
            JSON.serialize(new Map<String, String> { 'query' => query })
        );
        return (Map<String, Object>)JSON.deserializeUntyped(resBody);
    }
    
    public Map<String, Object> showJob(String jobId) {
        String resBody = this.sendHttpRequest('GET', '/v3/job/show/{job_id}'.replace('{job_id}', jobId), null);
        return (Map<String, Object>)JSON.deserializeUntyped(resBody);
    }
    
    public Map<String, Object> getStatus(String jobId) {
        String resBody = this.sendHttpRequest('GET', '/v3/job/status/{job_id}'.replace('{job_id}', jobId), null);
        return (Map<String, Object>)JSON.deserializeUntyped(resBody);
    }
    
    public String getResult(String jobId, String format) {
        return this.sendHttpRequest(
            'GET', 
            '/v3/job/result/{job_id}?format={format}'.replace('{job_id}', jobId)
                                                     .replace('{format}', format), 
            null
        );
    }
}

こう使う

TDClient client = new TDClient('{TD_API_KEY}');
client.issue(
    'presto', 
    'test_hoge',
    'test', 
    'SELECT COUNT(*) FROM test'    
);

//client.showJob('66400543');
//client.getStatus('66400543');
//client.getResult('66400543', 'json');
このエントリーをはてなブックマークに追加