CouchDBをいじってみる
インストールと起動
$ sudo port install couchdb $ sudo couchdb Apache CouchDB 0.9.0 (LogLevel=info) is starting. Apache CouchDB has started. Time to relax.
curlでアクセスしてみる
$ curl -v http://localhost:5984 * About to connect() to localhost port 5984 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 5984 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.19.5 (i386-apple-darwin9.6.0) libcurl/7.19.5 zlib/1.2.3 > Host: localhost:5984 > Accept: */* > < HTTP/1.1 200 OK < Server: CouchDB/0.9.0 (Erlang OTP/R13B) < Date: Thu, 04 Jun 2009 13:10:27 GMT < Content-Type: text/plain;charset=utf-8 < Content-Length: 40 < Cache-Control: must-revalidate < {"couchdb":"Welcome","version":"0.9.0"} * Connection #0 to host localhost left intact * Closing connection #0
ブラウザで http://localhost:5984/_utils/index.html にアクセスしてみる
curlでデータをいれたりしてみる。
DB作成
$ curl -v -X PUT http://localhost:5984/example * About to connect() to localhost port 5984 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 5984 (#0) > PUT /example HTTP/1.1 > User-Agent: curl/7.19.5 (i386-apple-darwin9.6.0) libcurl/7.19.5 zlib/1.2.3 > Host: localhost:5984 > Accept: */* > < HTTP/1.1 201 Created < Server: CouchDB/0.9.0 (Erlang OTP/R13B) < Date: Thu, 04 Jun 2009 13:26:13 GMT < Content-Type: text/plain;charset=utf-8 < Content-Length: 12 < Cache-Control: must-revalidate < {"ok":true} * Connection #0 to host localhost left intact * Closing connection #0
PUTでデータ挿入
$ curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"jiro\", \"age\":\"33\"}" http://localhost:5984/example/testdoc * About to connect() to localhost port 5984 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 5984 (#0) > PUT /example/testdoc HTTP/1.1 > User-Agent: curl/7.19.5 (i386-apple-darwin9.6.0) libcurl/7.19.5 zlib/1.2.3 > Host: localhost:5984 > Accept: */* > Content-Type: application/json > Content-Length: 27 > < HTTP/1.1 201 Created < Server: CouchDB/0.9.0 (Erlang OTP/R13B) < Etag: "1-2398975676" < Date: Thu, 04 Jun 2009 13:28:07 GMT < Content-Type: text/plain;charset=utf-8 < Content-Length: 48 < Cache-Control: must-revalidate < {"ok":true,"id":"testdoc","rev":"1-2398975676"} * Connection #0 to host localhost left intact * Closing connection #0
GETで挿入したデータの取得
$ curl -v -X GET http://localhost:5984/example/testdoc * About to connect() to localhost port 5984 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 5984 (#0) > GET /example/testdoc HTTP/1.1 > User-Agent: curl/7.19.5 (i386-apple-darwin9.6.0) libcurl/7.19.5 zlib/1.2.3 > Host: localhost:5984 > Accept: */* > < HTTP/1.1 200 OK < Server: CouchDB/0.9.0 (Erlang OTP/R13B) < Etag: "1-2398975676" < Date: Thu, 04 Jun 2009 13:29:07 GMT < Content-Type: text/plain;charset=utf-8 < Content-Length: 65 < Cache-Control: must-revalidate < {"_id":"testdoc","_rev":"1-2398975676","name":"jiro","age":"33"} * Connection #0 to host localhost left intact * Closing connection #0
DELETEでデータの削除
$ curl -v -X DELETE http://localhost:5984/example/testdoc?rev=1-2398975676 * About to connect() to localhost port 5984 (#0) * Trying 127.0.0.1... connected * Connected to localhost (127.0.0.1) port 5984 (#0) > DELETE /example/testdoc?rev=1-2398975676 HTTP/1.1 > User-Agent: curl/7.19.5 (i386-apple-darwin9.6.0) libcurl/7.19.5 zlib/1.2.3 > Host: localhost:5984 > Accept: */* > < HTTP/1.1 200 OK < Server: CouchDB/0.9.0 (Erlang OTP/R13B) < Etag: "2-2518059090" < Date: Thu, 04 Jun 2009 13:30:06 GMT < Content-Type: text/plain;charset=utf-8 < Content-Length: 48 < Cache-Control: must-revalidate < {"ok":true,"id":"testdoc","rev":"2-2518059090"} * Connection #0 to host localhost left intact * Closing connection #0
次にMapReduceを試すために以下の10件のデータを登録。名前と年齢です。
curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro1\", \"age\":\"11\"}" http://localhost:5984/test/person1 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro2\", \"age\":\"12\"}" http://localhost:5984/test/person2 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro3\", \"age\":\"21\"}" http://localhost:5984/test/person3 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro4\", \"age\":\"22\"}" http://localhost:5984/test/person4 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro5\", \"age\":\"23\"}" http://localhost:5984/test/person5 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro6\", \"age\":\"31\"}" http://localhost:5984/test/person6 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro7\", \"age\":\"32\"}" http://localhost:5984/test/person7 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro8\", \"age\":\"33\"}" http://localhost:5984/test/person8 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro9\", \"age\":\"34\"}" http://localhost:5984/test/person9 curl -v -X PUT -H "Content-Type: application/json" --data "{\"name\":\"taro10\", \"age\":\"35\"}" http://localhost:5984/test/person10
Map
function(doc) { if(doc.age > 30) { emit(null, 1); } }
Reduce
function(keys,values) { return sum(values); }
参考にしたもの