NodeJS & NoSQL PPT: http://goo.gl/nHQRJ Code: https://github.com/peihsinsu/nko2012.git Simon Su simonsu.mail@gmail.com
課程大綱 ● 三分鐘簡介NoSQL ● 初入NoSQL的選擇 ● Node.js使用CouchDB ● CouchDB Map / Reduce介紹 ● CouchDB Administration簡介
Why NoSQL - We Need Store... File Store (R)DBMS
We Need Store - Trouble in RDBMS From: http://www.slideshare.net/quipo/nosql-databases-why-what-and-when
What is NoSQL NoSQL = Not-Only-SQL
About NoSQL
NoSQL Size vs Complexity From: http://www.slideshare.net/quipo/nosql-databases-why-what-and-when
relax My Choice
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.
User Friendly of CouchDB ● Install: http://wiki.apache.org/couchdb/Installation configuration status check query user auth. management replocate REST server
User Friendly of CouchDB ● [GET] http://ipaddress:5984/database/column_key
CouchDB PaaS Services ● Cloudant http://cloudant.com ● IrisCouch http://www.iriscouch.com/
Node Knock Out 2012 Start to using NoSQL
Node.js NoSQL environment # express product # vi package.json (add cradle and other package relations...) # cd product # npm install https://github.com/cloudhead/cradle
Connect to NoSQL db = new(cradle.Connection)( db_address, db_port, {auth: { username: dbusername, password: dbpassword}, cache: true, raw: false }).database(databasename);
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);
Schema-Free var 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
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 all RDBMS data effected DML id (pk) force name sex skywalker light Luke Skywalker ... ... ... ...
NoSQL最佳使用方式 ● 離散資料 - 無特定規則可循 ● 原始資料 - 不正規化、不特別處理 ● 以Map / Reduce重新規劃資料呈現方式 ● 搭配RESTful使用(http://goo.gl/iEKDR)
Node Knock Out 2012 NoSQL Map / Reduce
View Model - Map / Reduce
View Model - Map / Reduce
Example Data - Guess Game Raw { "_id":"1350783674236", "_rev":"1-fed6ba9a837d66c8fb086b32294d5f7d", "sessionid":"1350783674236", "data":[ { "player":"3WAUD..EZOHQ", "name":"Caesar Chi", "score":2 } ] }
CouchDB Design - Map Function function(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資料
CouchDB Design - Reduce Function function(key, values, rereduce){ 將原資料型態({player: player_id, name: return sum(values); player_name}: score)其中的Score以作分 } 組(group by key)加總 Source: 以{user資訊: 答對題目數} 為資料型態的資料 Result: 以user資訊為群組,加總答對題目 數,建立相同資料型態的資料
Node Knock Out 2012 NoSQL Administration
Replicate Database Replicate
Single Server Replicate
CouchDB Cluster Service
Node Knock Out 2012 Q&A
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
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
Node Knock Out 2012 附錄
範例與執行 ● Code: test-couchdb.js ● Execute:
Node Knock Out 2012 After Class...
CouchDB Related ● CouchDB sorting related - View Collection http://wiki.apache.org/couchdb/View_collation? action=show&redirect=ViewCollation

Nko workshop - node js & nosql

  • 1.
    NodeJS & NoSQL PPT: http://goo.gl/nHQRJ Code: 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
  • 7.
  • 8.
    NoSQL Size vsComplexity From: http://www.slideshare.net/quipo/nosql-databases-why-what-and-when
  • 9.
  • 10.
    Why CouchDB ● Implementionfor 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.
  • 11.
    User Friendly ofCouchDB ● Install: http://wiki.apache.org/couchdb/Installation configuration status check query user auth. management replocate REST server
  • 12.
    User Friendly ofCouchDB ● [GET] http://ipaddress:5984/database/column_key
  • 13.
    CouchDB PaaS Services ●Cloudant http://cloudant.com ● IrisCouch http://www.iriscouch.com/
  • 14.
    Node Knock Out2012 Start to using NoSQL
  • 15.
    Node.js NoSQL environment #express product # vi package.json (add cradle and other package relations...) # cd product # npm install https://github.com/cloudhead/cradle
  • 16.
    Connect to NoSQL db= new(cradle.Connection)( db_address, db_port, {auth: { username: dbusername, password: dbpassword}, cache: true, raw: false }).database(databasename);
  • 17.
    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);
  • 18.
    Schema-Free var 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
  • 19.
    Schema mapping toRDBMS 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 all RDBMS data effected DML id (pk) force name sex skywalker light Luke Skywalker ... ... ... ...
  • 20.
    NoSQL最佳使用方式 ● 離散資料 - 無特定規則可循 ● 原始資料 - 不正規化、不特別處理 ● 以Map / Reduce重新規劃資料呈現方式 ● 搭配RESTful使用(http://goo.gl/iEKDR)
  • 21.
    Node Knock Out2012 NoSQL Map / Reduce
  • 22.
    View Model -Map / Reduce
  • 23.
    View Model -Map / Reduce
  • 24.
    Example Data -Guess Game Raw { "_id":"1350783674236", "_rev":"1-fed6ba9a837d66c8fb086b32294d5f7d", "sessionid":"1350783674236", "data":[ { "player":"3WAUD..EZOHQ", "name":"Caesar Chi", "score":2 } ] }
  • 25.
    CouchDB Design -Map Function function(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資料
  • 26.
    CouchDB Design -Reduce Function function(key, values, rereduce){ 將原資料型態({player: player_id, name: return sum(values); player_name}: score)其中的Score以作分 } 組(group by key)加總 Source: 以{user資訊: 答對題目數} 為資料型態的資料 Result: 以user資訊為群組,加總答對題目 數,建立相同資料型態的資料
  • 27.
    Node Knock Out2012 NoSQL Administration
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
    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
  • 33.
    Reference 2: SomeAcronyms ● 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
  • 34.
    Node Knock Out2012 附錄
  • 35.
  • 36.
    Node Knock Out2012 After Class...
  • 37.
    CouchDB Related ● CouchDB sorting related - View Collection http://wiki.apache.org/couchdb/View_collation? action=show&redirect=ViewCollation