Couch db 浅漫游.


Published on

Published in: Self Improvement
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Couch db 浅漫游.

  1. 1. • 这个个人网站使用CouchDB的技术资料 • • • • •
  2. 2. How to install couchDB on windows 安装地址 How to install cygwin on windows(辅助工具) 安装地址
  3. 3. What is REST? See REST简介 • GET: retrieve data from the database • PUT: insert new data and modify existing data • POST: equate to put, but it is not recommended in most cases • DELETE: delete data from the database • COPY: copy documents in the database 标准的 HTTP 方法:
  4. 4. Creating the database is simple curl -X PUT Get databases are currently available on the server curl -X GET delete special database on the server curl -X DELET fetch some information about the contacts database curl -X GET
  5. 5. //创建名字为kevin的文档 curl -X PUT -d '{}‘ //获取刚创建的文档 curl -X GET //逻辑删除文档 curl -X DELETE //是不是逻辑删除我们可以重新激活它,在这个删除的版本上更新操作 curl -X PUT -d '{"_rev":"?","email":["",""],"age":"23"}' //copy数据库(备份数据库) curl -X COPY -H "Destination: xiaoqiang"
  6. 6. 查询详细的生存的文档视图 curl -X GET (?include_docs=true)可选 how to get some meaningful data out of your CouchDB database, using the built- in JavaScript view engine. Using views, you can aggregate and report on the documents in your CouchDB database there is no restriction on the number of views you can have of any one document 视图本身就是一个特殊的文档
  7. 7. you wanted to retrieve the name, e-mail address, and fax number of all your users  SELECT name, email, fax FROM BMW_USER
  8. 8. Getting started wit CouchDB views let's translate this data into something that would make sense for CouchDB “Map function document”.
  9. 9. the information for user “Alice” 的JSON 数据结构
  10. 10. function(doc){ } Map function
  11. 11. function(doc) { if(doc.type == "user") { //Now we know we have a user document } } Map function
  12. 12. Now let's add in the SELECT * andWHERE username = 'Alice' parts together the key in our table will be the criteria we use to lookup the data we're interested in. Thus, we need to emit the user document's username as the key, and the entire Document as the value. function(doc) { if(doc.type == "user") { emit( doc.username, doc ); //Map functions convert documents into a hash table-like structure, and that's what this function will need to do. It will perform this task through a function called emit } } 这样一个简单map 函数编写完成了
  13. 13. Temporary view function(doc) { if(doc.fax && && emit(doc._id, {Name:, Phone:}); } execute a temporary view (限制使用) curl -X POST -d '{"map":"function(doc) {emit(doc._id, doc); }"}' -H 'Content- Type:application/json’ execute a permanent view curl -X GET curl -X GET 当然我们可以合并这二个视图,如既要按fax或email查询用户,取这2个视图的并
  14. 14. The raw view of the contacts database页面 输出标准的json格式,我们可以方便的ajax来获取数据) 如采用YUI封装的ajax写法, IE下打开测试页面 等待页面加载完成后直接获取数 据渲染页面
  15. 15. how CouchDB uses map/reduce views instead of SQL statements to interact with data ?
  16. 16. 如何归并相同的 key数据?See Reduce Function 点击 Run按钮执行
  17. 17. function(key, values, reduce){ } we know the reduce function works after the map function, this reduce function will process data from the Map function's hash table.  "key" argument is a singular key from the hash table  "value" is whatever we emitted as the value under that particular key.
  18. 18. the Reduce function is responsible for reducing the values under a particular key in the hash-table down into a smaller result set, potentially a single record. Making the Map a little more useable with Reduce
  19. 19. Map for reduce function(doc) { if(doc.type == "user") { emit( 1, doc ); } } Reduce counting users function(key, values, rereduce) { return values.length; } If we had 20 user records(documents), then the result would be {"1" : "20"}.
  20. 20. 当我选中下图reduce这个选择框时,就归并了相同的key记录,我这里的归并函数 是sum函数(注,按照自己的需求函数都可以自定义)
  21. 21. •Contain both an e-mail address and a fax number •Contain only an e-mail address •Contain only a fax number •Contain neither an e-mail address nor a fax number function(doc) { if( && doc.fax) emit("Both", 1); else if( emit("Email", 1); else if(doc.fax) emit("Fax", 1); else emit("Neither", 1); }
  22. 22. how you can aggregate this data to produce a count of the contacts for each key. The Reduce Function for the AggregateView function(key, values, reduce) { return sum(values); } 更新doucument,视图也会自动更新 //查看视图 curl -X GET 根据key分组 curl -X GET
  23. 23. SELECT id, name, email FROM contacts WHERE country = 'USA' ORDER BY name function(doc) { if(doc.type != "contact") return; emit([,], {name:, email:}); }["U SA"]&endkey=["USA",{}] SELECT COUNT(*), country FROM contacts GROUP BY country ===> map: function(doc) { emit(, 1); } reduce: function(key, values, rereduce) { return sum(values);
  24. 24. t_word?group=true
  25. 25. access Futon HomePage
  26. 26. http://localhost:5984/couchdb_in_action
  27. 27. If SQL is based on a rigid type system enforced by tables, then CouchDB is best described as a ducktyped system. Ducktyping comes from the expression "if it looks like a duck, talks like a duck, then it's probably a duck."
  28. 28. look like a Person
  29. 29. Transitioning more complex relationships to documents The three principal relationships
  30. 30. JSON
  31. 31. two relation document
  32. 32. document of merged one-to-many with type on the sub document /* Combined document */ { "username" : "chris", "addresses" : [ { "type" : "shipping" "address1" : "1234 N. Here St.", "city" : "Phoenix", "state" : "Arizona", "zip" : "85005" "country" : "us" } ] } Combined document one to one
  33. 33. Example document of merged one-to-many relationship with specific keysCombined document one to many
  34. 34. /* Denormalized many-to-many document { "username" : "chris", "followers" : [ {"user_id" : "", "username" : "trent"}, {"user_id" : "", "username" : "eve"} ], "following" : [ {"user_id" : "", "username" : "alice"}, {"user_id" : "", "username" : "bob"} ] }
  35. 35. 反规范化
  36. 36. Validating your data
  37. 37. 安装所需要的组件 CouchApp—a set of scripts that allow complete, stand-alone CouchDB applications to be built using just HTML and JavaScript.These applications are housed in the CouchDB database, meaning that when the database is replicated, any applications stored in that database are also replicated
  38. 38. A SimpleTask Manager 在命令行中进入F盘,敲Couchapp generate couchtasks,生成如图所示 在_attachments子目录中存放我们的web资源文件,我们可以加以修改 上传到服务器 couchapp push .
  39. 39. Advantages : flexibility and portability
  40. 40. using CouchDB as a traditional database back end to a serverside application developed in Python,Ruby, Django
  41. 41. from couchdbkit.client import Server server = Server() server.create_db(“python_test”) Couchdbkit的目标是为您的Python的应用提 供调用和管理Couchdb的框架。 网站地址:
  42. 42. 这个工具我也是用了一 会,我打算统一使用 jetBrains 提供的Python IDE: pycharm 下载链接 http://www.jetbrains. com/pycharm/
  43. 43. Couchrest是一个CouchDB RESTful Ruby客户端 网址地址: core
  44. 44. RubyMine下载链接地 址: m/ruby/download/index .html
  45. 45. 网址:
  46. 46. 我们需要在服务器上进行逻辑处理和创建网页,但现在这种 需要将会大幅下降,在极端情况下,服务器可能只需提供数 据库服务即可。 当然这只是假设在极端的情况下会如此。对于复杂的、需要 协调大量服务的、或对浏览器处理应用程序的安全性不放心 的企业应用程序,服务器软件将继续发挥不可或缺的作用。 但对服务于大众消费者的主流商业应用程序而言,“客户端 为重,服务器为轻”的前景似乎已无可置疑。
  47. 47. • •