nko workshop - node js & nosql
TRANSCRIPT
NodeJS & NoSQLPPT: http://goo.gl/nHQRJ
Code: https://github.com/peihsinsu/nko2012.git
Simon [email protected]
● 三分鐘簡介NoSQL● 初入NoSQL的選擇● Node.js使用CouchDB● CouchDB Map / Reduce介紹● CouchDB Administration簡介
課程大綱
Why NoSQL - We Need Store...
(R)DBMSFile Store
We Need Store - Trouble in RDBMS
From: http://www.slideshare.net/quipo/nosql-databases-why-what-and-when
NoSQL = Not-Only-SQL
What is NoSQL
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
query
replocateREST server
user auth.management
status check
User Friendly of CouchDB
● [GET] http://ipaddress:5984/database/column_key
CouchDB PaaS Services
● Cloudanthttp://cloudant.com
● IrisCouchhttp://www.iriscouch.com/
Start to using NoSQL
Node Knock Out 2012
Node.js NoSQL environment
# express product# vi package.json (add cradle and other package relations...)
# cd product# npm install
https://github.com/cloudhead/cradle
db = new(cradle.Connection)( db_address, db_port, {auth: { username: dbusername, password: dbpassword}, cache: true, raw: false}).database(databasename);
Connect to NoSQL
● [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);
NoSQL Basic CRUD
Schema-Free var cradle = require('cradle'); var db = new(cradle.Connection)().database('starwars'); db.save('skywalker', { force: 'light', name: 'Luke Skywalker' }, callback);
Key skywalker
Value force name
light Luke Skywalker
doc-key
document
starwars
Schema mapping to RDBMS
Key skywalker
Value force name ---
light Luke Skywalker ---
force name sex
light Richard W. M
NoSQL
id (pk) force name sex
skywalker light Luke Skywalker
... ... ... ...
RDBMS
Document to save, free schema and dynamic change.
Schema create first, insert later... Every column add, need schema update, and all data effected DML
NoSQL最佳使用方式
● 離散資料 - 無特定規則可循● 原始資料 - 不正規化、不特別處理● 以Map / Reduce重新規劃資料呈現方式● 搭配RESTful使用(http://goo.gl/iEKDR)
NoSQL Map / Reduce
Node Knock Out 2012
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 Functionfunction(doc) { if(doc.data){ for(var i = 0 ; i < doc.data.length ; i++ ) { if(doc.data[i].player && doc.data[i].score){ emit({player:doc.data[i].player,name:doc.data[i].name}, doc.data[i].score); } } }}
列舉data並以({player: player_id, name: player_name}: score) 建立View
Source:一筆scores資料
Result:一筆view資料
CouchDB Design - Reduce Functionfunction(key, values, rereduce){ return sum(values);}
將原資料型態({player: player_id, name: player_name}: score)其中的Score以作分組(group by key)加總
Source:以{user資訊: 答對題目數}為資料型態的資料
Result:以user資訊為群組,加總答對題目數,建立相同資料型態的資料
NoSQL Administration
Node Knock Out 2012
Replicate Database
Replicate
Single Server Replicate
CouchDB Cluster Service
Q & A
Node Knock Out 2012
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:
After Class...
Node Knock Out 2012
CouchDB Related● CouchDB sorting related - View Collection
http://wiki.apache.org/couchdb/View_collation?action=show&redirect=ViewCollation