1.什麼是NoSQL - Not only SQL
NoSQL 資料沒有模式 - 不需要事先定義資料結構(NoSQL is schema free — you don’t need to decide the structure up front.)
NoSQL 並不是萬能的,如果你是處理的是關係型資料,那仍然建議你使用RDBMS(Keep in mind that NoSQL is not a silver bullet. If your data is truly relational, sticking with your RDBMS would be the right choice.)
2.查詢NoSQL資料庫 - 映射/化簡(Map/Reduce)
什麼是Map/Reduce,為什麼Map/Reduce,請參見: http://wiki.huihoo.com/index.php?title=MapReduce
Map(映射)函數,用來把一組索引值對映射成一組新的索引值對 - List2 map(Functor1, List1)
Reduce(化簡)函數,指的是對一個列表的元素進行適當的合并 - Object reduce(Functor2, List2)
3.CrouchDB介紹 - CouchDB is a database designed to run on the internet of today.
- 她是面向文檔的JSON格資料庫,使用Erlang編寫。
- 她是高度並發資料庫,實現可拷貝、水平跨越多個裝置、可容錯(It is a highly concurrent database designed to be easily replicable, horizontally, across numerous devices and be fault tolerant.)
- 屬於NoSQL資料庫
- 她是一個開源Apache基金項目
- 她使用JSON儲存資料、並可以通過RESTful訪問資料
- 她使用映射/化簡來索引和查詢資料庫
1. 安裝:http://www.couchone.com/get
2. CrouchDB之初見 - Futon
http://127.0.0.1:5984/
http://127.0.0.1:5984/_utils/ (此頁面實際使用了JQuery外掛程式實現現,http://127.0.0.1:5984/_utils/script/jquery.couch.js,這正好是個學習如何與CrouchDB的很好的案例)
3. 使用者管理:
預設所有使用者都可以操作、設定資料庫,
建立管理員帳號,其它使用者只能讀寫資料,而不可以設定資料庫 (點擊Fix this)
最好不要只使用超級使用者來讀寫資料,因類這個超級用記的使用者名稱密碼在用戶端Javascript中很容易被看到 - $.couch.signup()
4.建立/修改文檔 - (New Document 、 Add Field(建議保留ID欄位)、Save Document)
5.使用cURL建立文檔:(POST, GET, PUT, DELETE)
curl -X POST http://127.0.0.1:5984/mycouchshop/ -d @person.json -H "Content-Type: application/json"
或者使用其它HTTP工具
6.使用cURL查看所有文檔
curl -X GET http://127.0.0.1:5984/mycouchshop/_all_docs
7.建立映射/化簡(Map/Reduce)函數
建立文檔:
{"name": "p1", "product": "web", "price": 10}
{"name": "p1", "product": "web", "price": 20}
{"name": "p1", "product": "portal", "price": 20}
Futon -> Temporary View
Map
function (doc) {
if (doc.type === "product" && doc.price) {
emit(doc.id, doc.price);
}
}
Redue
function (keys, prices) {
return sum(prices);
}
結果:
"web": 30
"portal": 20
http://net.tutsplus.com/tutorials/getting-started-with-couchdb/