2014-06-03
CouchDB也是NoSQL中的一员,使用Erlang OTP编写,以JSON的格式存储数据,现在是Apache的顶级项目。
安装
如果在windows下安装couchdb,在官网下载exe文件(笔者下载的版本是1.5.1),像平常的软件那样安装就行。安装后,会在Windows系统中添加Apache CouchDB
这一服务,以后的开启、关闭都可以使用服务的形式操作。当然,也可以使用安装目录下/bin
目录下的工具。
要在Linux下安装1.5.1,可以参考下面的步骤,笔者在Linux Mint 16下安装完成。
安装相关库:
sudo apt-get install -y g++
sudo apt-get install -y erlang-dev erlang-manpages erlang-base-hipe erlang-eunit erlang-nox erlang-xmerl erlang-inets
sudo apt-get install -y libmozjs185-dev libicu-dev libcurl4-gnutls-dev libtool
下面安装的并非必要,但是比较有用:
sudo apt-get install -y erlang-observer erlang-appmon erlang-debugger erlang-et
sudo apt-get install -y erlang-dialyzer erlang-percept erlang-typer erlang-edoc erlang-os-mon erlang-runtime-tools erlang-inviso erlang-tools
下载源码包,解压,进入解压后目录:
./configure && make
sudo make install
配置成可以使用service启动:
sudo rm /etc/logrotate.d/couchdb /etc/init.d/couchdb
sudo ln -s /usr/local/etc/logrotate.d/couchdb /etc/logrotate.d/couchdb
sudo ln -s /usr/local/etc/init.d/couchdb /etc/init.d
sudo update-rc.d couchdb defaults
这时候还不能正常启动。
添加用户couchdb:
adduser --system \
--home /usr/local/var/lib/couchdb \
--no-create-home \
--shell /bin/bash \
--group --gecos \
"CouchDB Administrator" couchdb
这时候,使用service启动,看似启动了,其实并没启动。如果以下面的命令启动CouchDB,
sudo -i -u couchdb couchdb
会出现init terminating in do_boot ()
类似的错误。所以,继续进行配置:
修改相关目录的所有者:
chown -R couchdb:couchdb /usr/local/etc/couchdb
chown -R couchdb:couchdb /usr/local/var/lib/couchdb
chown -R couchdb:couchdb /usr/local/var/log/couchdb
chown -R couchdb:couchdb /usr/local/var/run/couchdb
修改相关目录的权限:
chmod 0770 /usr/local/etc/couchdb
chmod 0770 /usr/local/var/lib/couchdb
chmod 0770 /usr/local/var/log/couchdb
chmod 0770 /usr/local/var/run/couchdb
使用service
启动CouchDB,
sudo service couchdb start
打开浏览器,访问http://127.0.0.1:5984/
,如果出现下面的类似信息,就成功了:
{"couchdb":"Welcome","uuid":"04a96d7d0348db60b45ccea96cef605b","version":"1.5.1","vendor":{"version":"1.5.1","name":"The Apache Software Foundation"}}
使用curl管理CouchDB
查看CouchDB的信息:
bash >> curl http://127.0.0.1:5984/
{
"couchdb": "Welcome",
"uuid": "04a96d7d0348db60b45ccea96cef605b",
"version": "1.5.1",
"vendor": {
"version": "1.5.1",
"name": "The Apache Software Foundation"
}
}
查看有哪些数据库:
bash >> curl http://127.0.0.1:5984/_all_dbs
["_replicator","_users"]
curl
默认使用GET
方法发起HTTP,所以也可以使用下面的方式查看有哪些数据库:
bash >> curl -X GET http://127.0.0.1:5984/_all_dbs
["_replicator","_users"]
创建数据库baseball:
bash >> curl -X PUT http://127.0.0.1:5984/baseball
{"ok":true} #表示创建成功
bash >> curl -X GET http://127.0.0.1:5984/_all_dbs
["_replicator","_users","baseball"]
如果创建成功的话,返回{"ok":true}
。
这时,如果再创建数据库baseball,会报错:
bash >> curl -X PUT http://127.0.0.1:5984/baseball
{
"error": "file_exists",
"reason": "The database could not be created, the file already exists."
}
查看数据库信息
bash >> curl -X GET http://127.0.0.1:5984/baseball
{
"db_name": "baseball",
"doc_count": 0,
"doc_del_count": 0,
"update_seq": 0,
"purge_seq": 0,
"compact_running": false,
"disk_size": 79,
"data_size": 0,
"instance_start_time": "1401782601538023",
"disk_format_version": 6,
"committed_update_seq": 0
}
删除数据库baseball
:
bash >> curl -X DELETE http://127.0.0.1:5984/baseball
{"ok":true}
bash >> curl -X GET http://127.0.0.1:5984/_all_dbs
["_replicator","_users"]
使用PUT方法在数据库albums下添加文档
建立数据库albums
:
bash >> curl -X PUT http://127.0.0.1:5984/albums
{"ok":true}
获取一个UUID:
bash >> curl -X GET http://127.0.0.1:5984/_uuids
{"uuids":["e30416a61033e6e488bf8e49c100022f"]}
要获取多个UUID,可以这样:
curl -X GET http://127.0.0.1:5984/_uuids?count=10
在albums下创建一个文档:
bash >> curl -X PUT http://127.0.0.1:5984/albums/776b03707a765a82fbd59f87060016a4 -d '{"title":"There is Nothing Left to Lose","artist":"Foo Fighters"}'
{
"ok": true,
"id": "776b03707a765a82fbd59f87060016a4",
"rev": "1-4b39c2971c9ad54cb37e08fa02fec636"
}
通过_id
查看指定的文档的内容
bash >> curl -X GET http://127.0.0.1:5984/albums/776b03707a765a82fbd59f87060016a4
{
"_id": "776b03707a765a82fbd59f87060016a4",
"_rev": "1-4b39c2971c9ad54cb37e08fa02fec636",
"title": "There is Nothing Left to Lose",
"artist": "Foo Fighters"
}
更新文档
以更新之前在albums中插入的文档为例子,这个文档的当前内容是:
{
"_id": "776b03707a765a82fbd59f87060016a4",
"_rev": "1-4b39c2971c9ad54cb37e08fa02fec636",
"title": "There is Nothing Left to Lose",
"artist": "Foo Fighters"
}
注意_rev
是couchdb为这个文档自动生成的版本号(revision number),能够看到-
之前的数字是1
。
下面的更新方法是错误的:
bash >> curl -X PUT http://127.0.0.1:5984/albums/776b03707a765a82fbd59f87060016a4 -d '{"title":"Hello","artist":"hi"}'
{"error":"conflict","reason":"Document update conflict."}
要解决这个问题,必须在新的文档中加入一个版本号:
bash >> curl -X PUT http://127.0.0.1:5984/albums/776b03707a765a82fbd59f87060016a4 -d '{"_rev":"1-4b39c2971c9ad54cb37e08fa02fec636","title":"Hello","artist":"hi"}'
{"ok":true,"id":"776b03707a765a82fbd59f87060016a4","rev":"2-d6d12edf5fbed99cacab10a93c9874bf"}
可以看到-
之前的数字是2
。在下次更新的时候需要用到这个新的版本号。
如何查看一个文档的多个版本的内容
先看一下有哪些版本:
bash >> curl -X GET http://127.0.0.1:5984/albums/776b03707a765a82fbd59f87060016a4?revs_info=true
{
"_id": "776b03707a765a82fbd59f87060016a4",
"_rev": "2-d6d12edf5fbed99cacab10a93c9874bf",
"title": "Hello",
"artist": "hi",
"_revs_info": [
{
"rev": "2-d6d12edf5fbed99cacab10a93c9874bf",
"status": "available"
},
{
"rev": "1-4b39c2971c9ad54cb37e08fa02fec636",
"status": "available"
}
]
}
查看版本1-4b39c2971c9ad54cb37e08fa02fec636
的内容:
bash >> curl -X GET http://127.0.0.1:5984/albums/776b03707a765a82fbd59f87060016a4?rev=1-4b39c2971c9ad54cb37e08fa02fec636
{
"_id": "776b03707a765a82fbd59f87060016a4",
"_rev": "1-4b39c2971c9ad54cb37e08fa02fec636",
"title": "There is Nothing Left to Lose",
"artist": "Foo Fighters"
}
简单的查询
在albums中再添加一个文档:
bash >> curl -X GET http://127.0.0.1:5984/_uuids
{"uuids":["e30416a61033e6e488bf8e49c100035f"]}
bash >> curl -X PUT http://127.0.0.1:5984/albums/e30416a61033e6e488bf8e49c100035f -d '{"title":"Letiantian","artist":"hi.ltt"}'
{"ok":true,"id":"e30416a61033e6e488bf8e49c100035f","rev":"1-462d3298f22df5850fd932fe124728c9"}
查询albums下的所有文档的信息:
bash >> curl -X GET http://127.0.0.1:5984/albums/_all_docs
{
"total_rows": 2,
"offset": 0,
"rows": [
{
"id": "776b03707a765a82fbd59f87060016a4",
"key": "776b03707a765a82fbd59f87060016a4",
"value": {
"rev": "2-d6d12edf5fbed99cacab10a93c9874bf"
}
},
{
"id": "e30416a61033e6e488bf8e49c100035f",
"key": "e30416a61033e6e488bf8e49c100035f",
"value": {
"rev": "1-462d3298f22df5850fd932fe124728c9"
}
}
]
}
使用浏览器查看内容
打开http://127.0.0.1:5984/_utils/
即可,界面简洁易懂。
参考资料
Install CouchDB on Ubuntu
Installation on Unix-like systems
Ubuntu下安装Apache CouchDB
couchDB启动报错
CouchDB The Definitive Guide