Your SlideShare is downloading. ×
0
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Couch db 浅漫游.
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Couch db 浅漫游.

1,164

Published on

Published in: Self Improvement
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,164
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. •http://www.elyservice.co.uk/ 这个个人网站使用CouchDB的技术资料 http://www.jasondavies.com/blog/2009/05/08/couchdb-on-wheels/ •https://github.com/langalex/boom_amazing •https://github.com/hpoydar/processing-js-studio •https://github.com/quirkey/swinger •https://github.com/jchris/couchdb-twitter-client •https://github.com/langalex/boom_amazing
  • 2. How to install couchDB on windows 安装地址 How to install cygwin on windows(辅助工具) 安装地址
  • 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. Creating the database is simple curl -X PUT http://127.0.0.1:5984/contacts Get databases are currently available on the server curl -X GET http://127.0.0.1:5984/_all_dbs delete special database on the server curl -X DELET http://127.0.0.1:5984/contacts fetch some information about the contacts database curl -X GET http://127.0.0.1:5984/contacts
  • 5. //创建名字为kevin的文档 curl -X PUT http://127.0.0.1:5984/contacts/kevin -d '{}‘ //获取刚创建的文档 curl -X GET http://127.0.0.1:5984/contacts/kevin //逻辑删除文档 curl -X DELETE http://127.0.0.1:5984/contacts/kevin?rev=? //是不是逻辑删除我们可以重新激活它,在这个删除的版本上更新操作 curl -X PUT http://127.0.0.1:5984/contacts/kevin -d '{"_rev":"?","email":["jinyuan@taobao.com","xiaoq@taobao.com"],"age":"23"}' //copy数据库(备份数据库) curl -X COPY http://127.0.0.1:5984/contacts/kevin -H "Destination: xiaoqiang"
  • 6. 查询详细的生存的文档视图 curl -X GET http://127.0.0.1:5984/contacts/_all_docs (?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. you wanted to retrieve the name, e-mail address, and fax number of all your users  SELECT name, email, fax FROM BMW_USER
  • 8. Getting started wit CouchDB views let's translate this data into something that would make sense for CouchDB “Map function document”.
  • 9. the information for user “Alice” 的JSON 数据结构
  • 10. function(doc){ } Map function
  • 11. function(doc) { if(doc.type == "user") { //Now we know we have a user document } } Map function
  • 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. Temporary view function(doc) { if(doc.fax && doc.name && doc.phone) emit(doc._id, {Name: doc.name, Phone: doc.phone}); } execute a temporary view (限制使用) curl -X POST http://127.0.0.1:5984/bmw_users/_temp_view -d '{"map":"function(doc) {emit(doc._id, doc); }"}' -H 'Content- Type:application/json’ execute a permanent view curl -X GET http://127.0.0.1:5984/bmw_users/_design/users/_view/get_fax_users curl -X GET http://127.0.0.1:5984/bmw_users/_design/users/_view/get_email_users 当然我们可以合并这二个视图,如既要按fax或email查询用户,取这2个视图的并
  • 14. The raw view of the contacts database http://127.0.0.1:5984/bmw_users/_design/users/_view/get_email_users(页面 输出标准的json格式,我们可以方便的ajax来获取数据) 如采用YUI封装的ajax写法, IE下打开测试页面 http://zhengke.org/project/test/ajax.html 等待页面加载完成后直接获取数 据渲染页面
  • 15. how CouchDB uses map/reduce views instead of SQL statements to interact with data ?
  • 16. 如何归并相同的 key数据?See Reduce Function 点击 Run按钮执行
  • 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. 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. 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. 当我选中下图reduce这个选择框时,就归并了相同的key记录,我这里的归并函数 是sum函数(注,按照自己的需求函数都可以自定义)
  • 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.email && doc.fax) emit("Both", 1); else if(doc.email) emit("Email", 1); else if(doc.fax) emit("Fax", 1); else emit("Neither", 1); }
  • 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 http://127.0.0.1:5984/bmw_users/_design/users/_view/count_by_type 根据key分组 curl -X GET http://127.0.0.1:5984/bmw_users/_design/users/_view/count_by_type?group=true
  • 23. SELECT id, name, email FROM contacts WHERE country = 'USA' ORDER BY name function(doc) { if(doc.type != "contact") return; emit([doc.country, doc.name], {name: doc.name, email: doc.email}); } http://127.0.0.1:5984/bmw_users/_design/users/_view/get_complex_key http://127.0.0.1:5984/bmw_users/_design/users/_view/get_complex_key?startkey=["U SA"]&endkey=["USA",{}] SELECT COUNT(*), country FROM contacts GROUP BY country ===> map: function(doc) { emit(doc.country, 1); } reduce: function(key, values, rereduce) { return sum(values);
  • 24. http://127.0.0.1:5984/doucuments/_design/words/_view/coun t_word?group=true
  • 25. access Futon HomePage http://127.0.0.1:5984/_utils
  • 26. http://localhost:5984/couchdb_in_action
  • 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. look like a Person
  • 29. Transitioning more complex relationships to documents The three principal relationships
  • 30. JSON
  • 31. two relation document
  • 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. Example document of merged one-to-many relationship with specific keysCombined document one to many
  • 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. 反规范化
  • 36. Validating your data
  • 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. A SimpleTask Manager 在命令行中进入F盘,敲Couchapp generate couchtasks,生成如图所示 在_attachments子目录中存放我们的web资源文件,我们可以加以修改 上传到服务器 couchapp push . http://127.0.0.1:5984/couchtasks
  • 39. Advantages : flexibility and portability
  • 40. using CouchDB as a traditional database back end to a serverside application developed in Python,Ruby, Django
  • 41. from couchdbkit.client import Server server = Server() server.create_db(“python_test”) Couchdbkit的目标是为您的Python的应用提 供调用和管理Couchdb的框架。 网站地址:http://couchdbkit.org/
  • 42. 这个工具我也是用了一 会,我打算统一使用 jetBrains 提供的Python IDE: pycharm 下载链接 http://www.jetbrains. com/pycharm/
  • 43. Couchrest是一个CouchDB RESTful Ruby客户端 网址地址: https://github.com/couchrest/couchrest/wiki/couchrest- core
  • 44. RubyMine下载链接地 址: http://www.jetbrains.co m/ruby/download/index .html
  • 45. 网址: http://www.djangoproject.com/
  • 46. 我们需要在服务器上进行逻辑处理和创建网页,但现在这种 需要将会大幅下降,在极端情况下,服务器可能只需提供数 据库服务即可。 当然这只是假设在极端的情况下会如此。对于复杂的、需要 协调大量服务的、或对浏览器处理应用程序的安全性不放心 的企业应用程序,服务器软件将继续发挥不可或缺的作用。 但对服务于大众消费者的主流商业应用程序而言,“客户端 为重,服务器为轻”的前景似乎已无可置疑。
  • 47. •http://couchdb.apache.org/ •http://wiki.apache.org/couchdb

×