5. How to install couchDB on windows
安装地址
How to install cygwin on windows(辅助工具)
安装地址
6. What is REST? See REST简介
• GET: retrieve data from the database
• PUT: insert new data and modify existing data
• POST: equate to put, but it is not recommended in most cases
• DELETE: delete data from the database
• COPY: copy documents in the database
标准的 HTTP 方法:
7. Creating the database is simple
curl -X PUT http://127.0.0.1:5984/contacts
Get databases are currently available on the server
curl -X GET http://127.0.0.1:5984/_all_dbs
delete special database on the server
curl -X DELET http://127.0.0.1:5984/contacts
fetch some information about the contacts database
curl -X GET http://127.0.0.1:5984/contacts
9. 查询详细的生存的文档视图
curl -X GET http://127.0.0.1:5984/contacts/_all_docs (?include_docs=true)可选
how to get some meaningful data out of your CouchDB database, using the built-
in JavaScript view engine.
Using views, you can aggregate and report on the documents in your CouchDB
database
there is no restriction on the number of views you can have of any one document
视图本身就是一个特殊的文档
10. you wanted to retrieve the name, e-mail address, and fax number of all your users
SELECT name, email, fax FROM BMW_USER
11.
12. Getting started wit CouchDB views
let's translate this data into something that would make sense for CouchDB “Map
function document”.
16. Now let's add in the SELECT * andWHERE username = 'Alice' parts together
the key in our table will be the criteria we use to lookup the data we're interested in.
Thus, we need to emit the user document's username as the key, and the entire
Document as the value.
function(doc)
{
if(doc.type == "user")
{
emit( doc.username, doc );
//Map functions convert documents into a hash table-like structure, and that's what
this function will need to do. It will perform this task through a function called emit
}
}
这样一个简单map 函数编写完成了
17. Temporary view
function(doc) {
if(doc.fax && doc.name && doc.phone)
emit(doc._id, {Name: doc.name, Phone: doc.phone});
}
execute a temporary view (限制使用)
curl -X POST http://127.0.0.1:5984/bmw_users/_temp_view -d
'{"map":"function(doc) {emit(doc._id, doc); }"}' -H 'Content-
Type:application/json’
execute a permanent view
curl -X GET
http://127.0.0.1:5984/bmw_users/_design/users/_view/get_fax_users
curl -X GET
http://127.0.0.1:5984/bmw_users/_design/users/_view/get_email_users
当然我们可以合并这二个视图,如既要按fax或email查询用户,取这2个视图的并
18.
19.
20.
21. The raw view of the contacts database
http://127.0.0.1:5984/bmw_users/_design/users/_view/get_email_users(页面
输出标准的json格式,我们可以方便的ajax来获取数据)
如采用YUI封装的ajax写法, IE下打开测试页面
http://zhengke.org/project/test/ajax.html 等待页面加载完成后直接获取数
据渲染页面
22.
23. how CouchDB uses map/reduce views instead of SQL
statements to interact with data ?
26. function(key, values, reduce){
}
we know the reduce function works after the map function,
this reduce function will process data from the Map function's
hash table.
"key" argument is a singular key from the hash table
"value" is whatever we emitted as the value under that particular key.
27. the Reduce function is responsible for reducing the
values under a particular key in the hash-table
down into a smaller result set, potentially a single
record.
Making the Map a little more useable with Reduce
28. Map for reduce
function(doc)
{
if(doc.type == "user")
{
emit( 1, doc );
}
}
Reduce counting users
function(key, values, rereduce)
{
return values.length;
}
If we had 20 user records(documents), then the result would be {"1" : "20"}.
31. •Contain both an e-mail address and a fax number
•Contain only an e-mail address
•Contain only a fax number
•Contain neither an e-mail address nor a fax number
function(doc) {
if(doc.email && doc.fax)
emit("Both", 1);
else if(doc.email)
emit("Email", 1);
else if(doc.fax)
emit("Fax", 1);
else
emit("Neither", 1);
}
32. how you can aggregate this data to produce a count of the contacts for each key.
The Reduce Function for the AggregateView
function(key, values, reduce) {
return sum(values);
}
更新doucument,视图也会自动更新
//查看视图
curl -X GET http://127.0.0.1:5984/bmw_users/_design/users/_view/count_by_type
根据key分组
curl -X GET
http://127.0.0.1:5984/bmw_users/_design/users/_view/count_by_type?group=true
33. SELECT id, name, email FROM contacts WHERE country = 'USA' ORDER BY name
function(doc) {
if(doc.type != "contact") return;
emit([doc.country, doc.name], {name: doc.name, email: doc.email});
}
http://127.0.0.1:5984/bmw_users/_design/users/_view/get_complex_key
http://127.0.0.1:5984/bmw_users/_design/users/_view/get_complex_key?startkey=["U
SA"]&endkey=["USA",{}]
SELECT COUNT(*), country FROM contacts GROUP BY country ===>
map: function(doc) {
emit(doc.country, 1);
}
reduce: function(key, values, rereduce) {
return sum(values);
38. If SQL is based on a rigid type system enforced by tables, then CouchDB is
best described as a ducktyped system. Ducktyping comes from the
expression "if it looks like a duck, talks like a duck, then it's probably
a duck."
43. document of merged one-to-many with type on the sub document
/* Combined document */
{
"username" : "chris",
"addresses" : [
{
"type" : "shipping"
"address1" : "1234 N. Here St.",
"city" : "Phoenix",
"state" : "Arizona",
"zip" : "85005"
"country" : "us"
}
]
}
Combined document
one to one
44. Example document of merged one-to-many relationship with specific keysCombined document
one to many
48. 安装所需要的组件
CouchApp—a set of scripts that allow complete, stand-alone CouchDB applications
to be built using just HTML and JavaScript.These applications are housed in the
CouchDB database, meaning that when the database is replicated, any
applications stored in that database are also replicated
52. using CouchDB as a traditional database back end to a
serverside application developed in Python,Ruby, Django
53. from couchdbkit.client import Server
server = Server()
server.create_db(“python_test”)
Couchdbkit的目标是为您的Python的应用提
供调用和管理Couchdb的框架。
网站地址:http://couchdbkit.org/