CouchDB的简单使用

一、安装CouchDB

     到官网下载CouchDB,在windows下安装CouchDB较为简单,略过。

      安装完后,确认CouchDB在运行,然后在浏览器访问http://127.0.0.1:5984/,正常情况不网页上显示

   

{
    "couchdb":"Welcome",
    "version":"2.1.1",
    "features":[
        "scheduler"
    ],
    "vendor":{
        "name":"The Apache Software Foundation"
    }
}

   与CouchDB交互:

       1.访问http://127.0.0.1:5984/_utils/去加载Fauxton,通过Fauxton可与CouchDB交互

       2.使用curl实用程序和CouchDB交互

                   安装curl实用程序。如果下载的是安装版curl默认情况下会自动配置环境变量,如果是解压版请自行配置环境变量(即把安装目录的bin路径添加到path下)。完成后在cmd中输入curl,有正常反应则表示安装成功

二、CouchDB

      1.创建数据库

        由于通过Fauxton操作CouchDB比较简单,这里不详细介绍这种方法。主要介绍通过curl操作CouchDB。

         可以通过使用PUT方法通过curl实用程序向服务器发送HTTP请求,在CouchDB中创建数据库。 

            

curl -X PUT http://127.0.0.1:5984/albums

        CouchDB返回:

{"ok":true}

          这样一个名为albums的数据库就创建成功。-X是告诉curl发送什么请求(GET、PUT、DELETE、POST)。默认情况是发送GET请求。如

       

curl -X GET http://127.0.0.1:5984

curl  http://127.0.0.1:5984

效果是一样的。如果想知道请求响应的详细信息可加上-v(例如:curl -vX GET)。

       2.删除数据库

             

curl -X DELETE http://127.0.0.1:5984/albums

         CouchDB返回:

{"ok":true}

 

       3.创建文档

      

curl -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af -d '{"title":"There is Nothing Left to Lose","artist":"Foo Fighters"}'

     

6e1295ed6c29495e54cc05947f18c8af是文档id(建议用UUID或GUID)。-d后面是请求要发送的数据,要求Field-Value对的形式。
如果你没有UUID可通过curl -X GET http://127.0.0.1:5984/_uuids获得。如果你需要更多UUID,可以通过?count=10参数获得10个UUID,或者获取你想要的数目。

     CouchDB返回:

{"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"1-2902191555"}

     rev,表示修订ID。 每次修改(更新或修改)文档时,CouchDB都会生成_rev值。如果要更新或删除文档,CouchDB希望您包括要更改的版本的_rev字段。此机制用于确保并发控制。

注意:在windows下创建文档应这样输入:

curl -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af -d "{\"title\":\"There is Nothing Left to Lose\",\"artist\":\"Foo Fighters\"}"

否则会报如下错误:

{"error":"bad_request","reason":"invalid UTF-8 JSON"}

参考:https://andrewlocatelliwoodcock.wordpress.com/2011/05/11/curl-returning-invalid-utf-8-json-error-from-couchdb-on-windows-although-json-is-correct/

      3.更新文档

        可通过curl -X GET http://127.0.0.1/albums/6e1295ed6c29495e54cc05947f18c8af查到修订版本id再执行

curl -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af \
     -d '{"_rev":"1-2902191555","title":"There is Nothing Left to Lose","artist":"Foo Fighters","year":"1997"}'

  _rev为从文档中查出来的修订版本id。

    CouchDB返回

{"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"2-8aff9ee9d06671fa89c99d20a4b3ae"}

 

     4.删除文档

  

curl -X DELETE http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af?rev=4-d56957735aa2d7f352044103f39912c9

CouchDB返回

{"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"5-5c8ce4ce0709421945867b0e05cb19c4"}

      验证删除:

curl -X GET http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af

CouchDB返回

{"error":"not_found","reason":"deleted"}

   

5.附加文件

    

curl -X PUT http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg?rev=3-1b29c1219b6d80a8f7ded1ddfebb69ed --data-binary @E:\artwork.jpg -H "Content-Type:image/jpg"

上述命令通过PUT请求将E:\artwork.jpg附加到albums/6e1295ed6c29495e54cc05947f18c8af中,并命名为artwork.jpg。--data-binary @是告诉curl读文件内容到HTTP请求体, -H告诉CouchDB,我们上传的是JPEG文件。

CouchDB返回

{"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af","rev":"4-d56957735aa2d7f352044103f39912c9"}

可通过http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af/artwork.jpg访问附加文件。

再次请求文档

     

curl http://127.0.0.1:5984/albums/6e1295ed6c29495e54cc05947f18c8af

CouchDB返回

    

{
    "_id": "6e1295ed6c29495e54cc05947f18c8af",
    "_rev": "3-131533518",
    "title": "There is Nothing Left to Lose",
    "artist": "Foo Fighters",
    "year": "1997",
    "_attachments": {
        "artwork.jpg": {
            "stub": true,
            "content_type": "image/jpg",
            "length": 52450
        }
    }
}

 

 

 

参考链接:

             1.http://docs.couchdb.org/en/2.1.1/intro/tour.html

             2.http://docs.couchdb.org/en/2.1.1/intro/api.html

             3.https://andrewlocatelliwoodcock.wordpress.com/2011/05/11/curl-returning-invalid-utf-8-json-error-from-couchdb-on-windows-although-json-is-correct/

      

    

posted @ 2017-12-15 16:05  二十亿光年的孤独  阅读(625)  评论(0编辑  收藏  举报