Treasure DataのAPIを叩くときは各種言語のライブラリを利用するのがほとんどだと思いますが、REST APIを実際に叩いて何が出来るのか確認してみましたー
まずはcurlで叩いてみる
認証はAPI KeyをAuthorizationヘッダに入れるシンプルな方式。
ジョブの発行はこんな感じ
1 2 3 4 5 6 7 8 |
$ 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}' |
レスポンス
1 2 3 4 5 |
{ "job":"{job_id}", "database":"test_hoge", "job_id":"{job_id}" } |
ジョブのステータスを確認する
1 2 3 |
$ curl -i -X GET \ -H "Authorization:TD1 {TREASURE_DATA_API_KEY}" \ 'https://api.treasuredata.com/v3/job/status/{job_id}' |
レスポンス
1 2 3 4 5 6 7 8 9 10 11 |
{ "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" } |
結果を取得する
1 2 3 |
$ curl -i -X GET \ -H "Authorization:TD1 {TREASURE_DATA_API_KEY}" \ 'https://api.treasuredata.com/v3/job/result/{job_id}?format=csv' |
レスポンス。CSVだとこんな感じ
1 2 3 4 |
NOT RT,true,526 RT,true,5209 RT,false,15934 NOT RT,false,5907 |
Apexで書いてみる
Apexを使うとこんな感じで書けます。リモートサイトの設定も忘れずに。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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 ); } } |
こう使う
1 2 3 4 5 6 7 8 9 10 11 |
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'); |
コメントを残す