npm search riakしてみるとしてみるといくつか見つかるが、今回はふたつ。riak-jsとnode_riakだ。前者は有志が集まってwatch/forkともに一番多い。後者はVoxerが使っているということでおそらく安定はしていそうだが更新が随分前に止まっているのが心配だ。node.jsのプロたちには申し訳ないがむしろ私にとっての入門のような形になってしまった。
まずはもろもろ準備。npmはBundlerのようにも動くということなのでまずは
$ mkdir testproj
$ npm init
....
これでレポジトリの準備ができる。package.jsonは次のような形
{ "name": "testproj", "version": "0.0.0", "description": "Client test for Riak distributed database", "main": "index.js", "scripts": { "test": "test" }, "repository": { "type": "git", "url": "http://github.com/kuenishi/testproj" }, "keywords": [ "riak" ], "dependencies":{ "riak" : "*" }, "author": "Kota Uenishi", "license": "BSD" }
これで、
$ npm install $ npm install node-dev
としておく。node.jsを入れるとnodeコマンドが用意されるがこれはどうも本番用っぽいのでnode-devというものを入れる。これで試しに
var RiakClient = require("riak"), client; client = new RiakClient(["localhost:8098"], "client_id", "pool_name"); client.debug_mode = true; console.time('simpleget'); client.put("airlines", "ARG", {hello: "world"}, {}, function(e,r,o){} ); client.get("airlines", "ARG", {}, function (error, res, obj) { // `type` is either "histogram" or "counter". // Information is gleaned from requests made by poolee, and tells you about // downed nodes, retries, and request duration. console.log(error); console.log(res.headers); console.log(res.url); console.log(res.statusCode); console.log(res.bodyChunks); console.log(res.bodyLength); console.log(obj); }); console.timeEnd('simpleget');
などとしてみる。そうするとホラ、
$ node_modules/node-dev/node-dev index.js [master ~/src/riak-jsx-client] riak request put bucket_key="airlines/ARG" options: {"http_headers":{"X-Riak-ClientId":"client_id","Connection":"keep-alive","Content-Type":"application/json"},"method":"put","body":{"hello":"world"}} riak request pool options: {"path":"/riak/airlines/ARG","headers":{"X-Riak-ClientId":"client_id","Connection":"keep-alive","Content-Type":"application/json"},"retry_not_found":true} riak request get bucket_key="airlines/ARG" options: {"method":"get"} riak request pool options: {"path":"/riak/airlines/ARG","headers":{"X-Riak-ClientId":"client_id","Connection":"keep-alive"},"retry_not_found":true} simpleget: 16ms null { 'x-riak-vclock': 'a85hYGBgzGDKBVIcypz/fgYae5zPYEpUzWNlWHhv+Sm+LAA=', vary: 'Accept-Encoding', server: 'MochiWeb/1.1 WebMachine/1.9.2 (someone had painted it blue)', 'last-modified': 'Sun, 03 Mar 2013 14:48:01 GMT', etag: '"5pDQ94RnYlbHQpi2uz0FVw"', date: 'Sun, 03 Mar 2013 14:48:05 GMT', 'content-type': 'application/json', 'content-length': '17' } 200 [ <Buffer 7b 22 68 65 6c 6c 6f 22 3a 22 77 6f 72 6c 64 22 7d> ] 17 { hello: 'world' } riak req empty bucket_key="airlines/ARG" statusCode: 204 {"http_headers":{"X-Riak-ClientId":"client_id","Connection":"keep-alive","Content-Type":"application/json"},"method":"put","body":"{\"hello\":\"world\"}"}
結果がとれる。知らなかったのだけど、 "node index.js"とだけして実行するとそのままコールバックがセットされたサーバーが起動してしまうらしく、コンソールのコントロールが返ってこないので注意。これだと、jsonで入れておくとそのまま扱えるので便利ですね。