CrouchDB (NoSQL) 和使用 Map/Reduce 查詢資料庫 入門

來源:互聯網
上載者:User

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/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.