•http://www.elyservice.co.uk/ 这个个人网站使用CouchDB的技术资料
http://www.jasondavies.com/blog/2009/05/08/couchdb-on-wheels/
•https://github.com/langalex/boom_amazing
•https://github.com/hpoydar/processing-js-studio
•https://github.com/quirkey/swinger
•https://github.com/jchris/couchdb-twitter-client
•https://github.com/langalex/boom_amazing
How to install couchDB on windows
安装地址
How to install cygwin on windows(辅助工具)
安装地址
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 方法:
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
//创建名字为kevin的文档
curl -X PUT http://127.0.0.1:5984/contacts/kevin -d '{}‘
//获取刚创建的文档
curl -X GET http://127.0.0.1:5984/contacts/kevin
//逻辑删除文档
curl -X DELETE http://127.0.0.1:5984/contacts/kevin?rev=?
//是不是逻辑删除我们可以重新激活它,在这个删除的版本上更新操作
curl -X PUT http://127.0.0.1:5984/contacts/kevin -d
'{"_rev":"?","email":["jinyuan@taobao.com","xiaoq@taobao.com"],"age":"23"}'
//copy数据库(备份数据库)
curl -X COPY http://127.0.0.1:5984/contacts/kevin -H "Destination: xiaoqiang"
查询详细的生存的文档视图
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
视图本身就是一个特殊的文档
you wanted to retrieve the name, e-mail address, and fax number of all your users
 SELECT name, email, fax FROM BMW_USER
Getting started wit CouchDB views
let's translate this data into something that would make sense for CouchDB “Map
function document”.
the information for user “Alice” 的JSON 数据结构
function(doc){
}
Map function
function(doc)
{
if(doc.type == "user")
{
//Now we know we have a user document
}
}
Map function
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 函数编写完成了
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个视图的并
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 等待页面加载完成后直接获取数
据渲染页面
how CouchDB uses map/reduce views instead of SQL
statements to interact with data ?
如何归并相同的
key数据?See
Reduce Function
点击 Run按钮执行
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.
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
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"}.
当我选中下图reduce这个选择框时,就归并了相同的key记录,我这里的归并函数
是sum函数(注,按照自己的需求函数都可以自定义)
•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);
}
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
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);
http://127.0.0.1:5984/doucuments/_design/words/_view/coun
t_word?group=true
access Futon HomePage
http://127.0.0.1:5984/_utils
http://localhost:5984/couchdb_in_action
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."
look like a Person
Transitioning more complex relationships to documents
The three principal relationships
JSON
two relation document
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
Example document of merged one-to-many relationship with specific keysCombined document
one to many
/* Denormalized many-to-many document
{
"username" : "chris",
"followers" : [
{"user_id" : "", "username" : "trent"},
{"user_id" : "", "username" : "eve"}
],
"following" : [
{"user_id" : "", "username" : "alice"},
{"user_id" : "", "username" : "bob"}
]
}
反规范化
Validating your data
安装所需要的组件
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
A SimpleTask Manager
在命令行中进入F盘,敲Couchapp generate couchtasks,生成如图所示
在_attachments子目录中存放我们的web资源文件,我们可以加以修改
上传到服务器 couchapp push . http://127.0.0.1:5984/couchtasks
Advantages : flexibility and portability
using CouchDB as a traditional database back end to a
serverside application developed in Python,Ruby, Django
from couchdbkit.client import Server
server = Server()
server.create_db(“python_test”)
Couchdbkit的目标是为您的Python的应用提
供调用和管理Couchdb的框架。
网站地址:http://couchdbkit.org/
这个工具我也是用了一
会,我打算统一使用
jetBrains 提供的Python
IDE: pycharm
下载链接
http://www.jetbrains.
com/pycharm/
Couchrest是一个CouchDB RESTful Ruby客户端
网址地址:
https://github.com/couchrest/couchrest/wiki/couchrest-
core
RubyMine下载链接地
址:
http://www.jetbrains.co
m/ruby/download/index
.html
网址: http://www.djangoproject.com/
我们需要在服务器上进行逻辑处理和创建网页,但现在这种
需要将会大幅下降,在极端情况下,服务器可能只需提供数
据库服务即可。
当然这只是假设在极端的情况下会如此。对于复杂的、需要
协调大量服务的、或对浏览器处理应用程序的安全性不放心
的企业应用程序,服务器软件将继续发挥不可或缺的作用。
但对服务于大众消费者的主流商业应用程序而言,“客户端
为重,服务器为轻”的前景似乎已无可置疑。
•http://couchdb.apache.org/
•http://wiki.apache.org/couchdb
Couch db 浅漫游.

Couch db 浅漫游.