浅入浅出CouchDB:使用curl管理CouchDB


#其他


2014-06-03

笔者使用的CouchDB版本是1.5.1。

CouchDB支持以RESTFULL URL的方式操作数据库,关于restfull url,可以参考一下阮一峰的RESTful API 设计指南。在Linux下curl是一个强大的HTTP交互工具,常用来下载和测试网络。本文的内容就是使用curl通过CouchDB的RESTful API对CouchDB进行简单地管理,参考自官方文档。

下文中bash >>是shell提示符,大部分Linux用户的shell提示符是$

查看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/即可,界面简洁易懂。

题外话:一份好资料

CouchDB The Definitive Guide


( 本文完 )