Nko workshop - node js & nosql

Uploaded on


More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. NodeJS & NoSQL PPT: http://goo.gl/nHQRJCode: https://github.com/peihsinsu/nko2012.git Simon Su simonsu.mail@gmail.com
  • 2. 課程大綱● 三分鐘簡介NoSQL● 初入NoSQL的選擇● Node.js使用CouchDB● CouchDB Map / Reduce介紹● CouchDB Administration簡介
  • 3. Why NoSQL - We Need Store... File Store (R)DBMS
  • 4. We Need Store - Trouble in RDBMS From: http://www.slideshare.net/quipo/nosql-databases-why-what-and-when
  • 5. What is NoSQL NoSQL = Not-Only-SQL
  • 6. About NoSQL
  • 7. NoSQL Size vs Complexity From: http://www.slideshare.net/quipo/nosql-databases-why-what-and-when
  • 8. relaxMy Choice
  • 9. Why CouchDB● Implemention for ACID Properties ○ Multi-Version Concurrency Control (MVCC) ○ B-Tree indexes● Schema-Free document-oriented database● RESTful default support (JSON document)● View model / JavaScript View Functions● Replication (Peer-based distributed databases), Distributed, featuring robust, incremental replication with bi-directional conflict detection and management.
  • 10. User Friendly of CouchDB ● Install: http://wiki.apache.org/couchdb/Installationconfiguration status check query user auth. management replocate REST server
  • 11. User Friendly of CouchDB● [GET] http://ipaddress:5984/database/column_key
  • 12. CouchDB PaaS Services● Cloudant http://cloudant.com● IrisCouch http://www.iriscouch.com/
  • 13. Node Knock Out 2012Start to using NoSQL
  • 14. Node.js NoSQL environment# express product# vi package.json (add cradle and other package relations...)# cd product# npm install https://github.com/cloudhead/cradle
  • 15. Connect to NoSQLdb = new(cradle.Connection)( db_address, db_port, {auth: { username: dbusername, password: dbpassword}, cache: true, raw: false}).database(databasename);
  • 16. NoSQL Basic CRUD● [C] 新增一筆資料,id=PK,doc=欲儲存文件,callback=回傳 觸發事件: db.save(id, doc, callback);● [R] 查詢資料,id=欲查詢的資料id,callback=同上: db.get(id, callback);● [U] 修改一筆資料,id=欲修改之資料id,doc=欲修改的文件 內容,callback同上: db.merge(id, doc, callback);● [D] 刪除資料,id=PK,rev=版本號碼,callback同上: db.remove(id, rev, callback);
  • 17. Schema-Freevar cradle = require(cradle);var db = new(cradle.Connection)().database(starwars);db.save(skywalker, doc-key{ force: light, name: Luke Skywalker document}, callback); starwars Key skywalker Value force name light Luke Skywalker
  • 18. Schema mapping to RDBMS Document to save, free schema and dynamic NoSQL change. Key skywalker Value force name --- light Luke Skywalker --- force name sex light Richard W. M Schema create first, insert later... Every column add, need schema update, and allRDBMS data effected DML id (pk) force name sex skywalker light Luke Skywalker ... ... ... ...
  • 19. NoSQL最佳使用方式● 離散資料 - 無特定規則可循● 原始資料 - 不正規化、不特別處理● 以Map / Reduce重新規劃資料呈現方式● 搭配RESTful使用(http://goo.gl/iEKDR)
  • 20. Node Knock Out 2012NoSQL Map / Reduce
  • 21. View Model - Map / Reduce
  • 22. View Model - Map / Reduce
  • 23. Example Data - Guess Game Raw{ "_id":"1350783674236", "_rev":"1-fed6ba9a837d66c8fb086b32294d5f7d", "sessionid":"1350783674236", "data":[ { "player":"3WAUD..EZOHQ", "name":"Caesar Chi", "score":2 } ]}
  • 24. CouchDB Design - Map Functionfunction(doc) { if(doc.data){ 列舉data並以 for(var i = 0 ; i < doc.data.length ; i++ ) { ({player: player_id, name: player_name}: score) if(doc.data[i].player && doc.data[i].score){ 建立View emit({player:doc.data[i].player,name:doc.data[i].name}, doc.data[i].score); } } }}Source:一筆scores資料 Result: 一筆view資料
  • 25. CouchDB Design - Reduce Functionfunction(key, values, rereduce){ 將原資料型態({player: player_id, name: return sum(values); player_name}: score)其中的Score以作分} 組(group by key)加總 Source: 以{user資訊: 答對題目數} 為資料型態的資料 Result: 以user資訊為群組,加總答對題目 數,建立相同資料型態的資料
  • 26. Node Knock Out 2012NoSQL Administration
  • 27. Replicate Database Replicate
  • 28. Single Server Replicate
  • 29. CouchDB Cluster Service
  • 30. Node Knock Out 2012 Q&A
  • 31. Reference● 不做NoSQL的CouchDB: http://www.openfoundry.org/tw/tech-column/8301--nosql-couchdb● nosql-databases-why-what-and-when: http://www.slideshare.net/quipo/nosql-databases-why- what-and-when● Neo4J: http://neo4j.tw/● Couchdb REST samples: http://peihsinsu.blogspot.tw/search?q=couchdb● Couch View: http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views
  • 32. Reference 2: Some Acronyms● RDBMS: Relational Database Management System.● SQL: Structured Query Language, also used to refer to databases that use SQL as their query language.● NoSQL: Also called Distributed Database Management Systems, used to refer to a class of databases that are non-relational and do not use SQL as their query language.● ACID: Atomicity, Consistency, Isolation, Durability.● CAP: Consistency, Availability, Partition tolerance.● MVCC: Multi-Version Concurency Control
  • 33. Node Knock Out 2012 附錄
  • 34. 範例與執行● Code: test-couchdb.js● Execute:
  • 35. Node Knock Out 2012 After Class...
  • 36. CouchDB Related● CouchDB sorting related - View Collection http://wiki.apache.org/couchdb/View_collation? action=show&redirect=ViewCollation