IBM曾经资助CouchDB,允许Katz全职从事项目的开发。
2009年,Katz和Chris Anderson等 一些同仁组建Relaxed公司,同年11月公司获得200万美元风险投资, 改名为Couchio。
获 取CouchDB server info:
curl http://127.0.0.1:5984/{"couchdb":"Welcome","version":"0.11.0"}
创 建DB:
curl -X PUT http://127.0.0.1:5984/wikiCouchDB will reply with the following message, if the database does not exist:
{"ok":true}or, with a different response message, if the database already exists:
{"error":"file_exists","reason":"The database could not be created, the file already exists."}获取DB 信息:
curl -X GET http://127.0.0.1:5984/wiki{"db_name":"wiki","doc_count":0,"doc_del_count":0,"update_seq":0,
删 除DB:
curl -X DELETE http://127.0.0.1:5984/wiki{"ok":true}
map |
function(doc) { emit(null, doc); } |
To be able to filter or sort the view by some document property, you would use that property for the key. For example, the following view would allow you to lookup customer documents by the LastName or FirstName fields:
function(doc) {
reduce |
function (key, values, rereduce) { return sum(values); } |
Reduce functions must handle two cases:
1. When rereduce is false:
key will be an array whose elements are arrays of the form [key,id], where key is a key emitted by the map function and id is that of the document from which the key was generated.
values will be an array of the values emitted for the respective elements in keys
i.e. reduce([ [key1,id1], [key2,id2], [key3,id3] ], [value1,value2,value3], false)
2. When rereduce is true:
key will be null
values will be an array of values returned by previous calls to the reduce function
i.e. reduce(null, [intermediate1,intermediate2,intermediate3], true)
[X, Y, 0] -> Object_A然后,reduce函数会受到如下3个调用. ()
[X, Y, 1] -> Object_B
[X, Y, 2] -> Object_C
[X, Y, 3] -> Object_D
Introduction_to_CouchDB_views An introduction to views
HTTP_view_API How to use views
View_Snippets 最有用 。 -Lin Yang 7/23/10 11:58 AM
eCouch
erlcouch ..
CouchDB: The Definitive Guide 的翻译blog by 时之刻痕
http://wiki.apache.org/couchdb/FrontPage 总文档!!!
clients:
副自己的测试代码:
#!/usr/bin/python #coding:utf-8 # example: # do_request('10.99.60.91:8080', '/home', 'PUT', '', {"Content-type": "application/x-www-form-urlencoded"} ) def do_request2(netloc, path, method, data='', headers={}): import httplib conn = httplib.HTTPConnection(netloc) conn.request(method, path, data, headers) response = conn.getresponse() if response.status/100 == 2: data = response.read() return data print 'ERROR: response.status = %d'% response.status print 'response data is ', response.read() def do_request(url, method, data='', headers={}): print '>>>>>>>>>>>>> do_request: ', url from urlparse import urlparse o = urlparse(url) path = o.path if o.query: path = path + '?' + o.query return do_request2(o.netloc, path, method, data, headers) def delete_db(db_name): return do_request('http://127.0.0.1:5984/%s'%db_name, 'DELETE') def create_db(db_name): return do_request('http://127.0.0.1:5984/%s'%db_name, 'PUT') def create_doc(db_name, doc_id, doc): print 'create_doc %s ' % doc_id return do_request('http://127.0.0.1:5984/%s/%s'%(db_name, doc_id), 'PUT', doc, {'Content-Type': 'application/json'}) def get_doc(db_name, doc_id): return do_request('http://127.0.0.1:5984/%s/%s'%(db_name, doc_id), 'GET', '', {}) #query_string='?group=false' def query_temp_view(db_name, doc, query_string=''): url = 'http://127.0.0.1:5984/%s/_temp_view%s'%(db_name, query_string) return do_request(url, 'POST', doc, {'Content-Type': 'application/json; charset=UTF-8'}) # 这个API是创建好多个view,一个design document def create_permanent_view(db_name, view_name, views_json): return create_doc(db_name, view_name, views_json) def test(): print delete_db('phone') print create_db('phone') print create_doc('phone', 'Nokia-5200',''' {"make": "Nokia", "price": 100, "os": "s40"} ''') print create_doc('phone', 'Nokia-1661',''' {"make": "Nokia", "price": 32.5, "os": "s40"} ''') print create_doc('phone', 'Nokia-E63',''' {"make": "Nokia", "price": 500, "os": "s60"} ''') print create_doc('phone', 'HTC-Wildfire',''' {"make": "HTC", "price": 200, "os": "Android"} ''') print create_doc('phone', 'BlackBerry-Bold',''' {"make": "BlackBerry", "price": 300, "os": "BlackBerry-OS"} ''') print create_doc('phone', 'Samsung-Galaxy-S',''' {"make": "Samsung", "price": 400, "os": "Android"} ''') print create_doc('phone', 'iPhone4',''' {"make": "Apple", "price": 1000, "os": "Mac"} ''') ############################################################################ # get all docs view = ''' { "map" : "function(doc){ emit(doc.price, doc); }" } ''' print query_temp_view('phone', view); ############################################################################ # select sum(price) form phone view = ''' { "map" : "function(doc){ emit('all-price', doc.price); }", "reduce" : "function(key, values, rereduce){ return sum(values); }" } ''' print query_temp_view('phone', view); ############################################################################ # test permanent_view views = ''' { "language": "javascript", "views": { "all_phones": { "map" : "function(doc){ emit(doc.price, doc); }" }, "sum_price": { "map" : "function(doc){ emit('all-price', doc.price); }", "reduce" : "function(key, values, rereduce){ return sum(values); }" }, } } ''' create_permanent_view('phone', '_design/my_views', views) print ':::: retrive the views DOC: ' print get_doc('phone', '_design/my_views') print ':::: now let us try to query on this views "all_phones"' print get_doc('phone', '_design/my_views/_view/all_phones') print ':::: And query on this views "sum_price"' print get_doc('phone', '_design/my_views/_view/sum_price') ############################################################################ print ':::: we use temp view for test' print ':::: get all phones of Nokia: ' view = ''' { "map" : "function(doc){ //log(doc); // debug fun if (doc.make == 'Nokia') emit(null, doc); }" } ''' print query_temp_view('phone', view); ############################################################################ print ':::: get phones count of every os : ' view = ''' { "map" : "function(doc){ emit(doc.os, 1); }", "reduce" : "function(key, values, rereduce){ log('reduce called!!!!'); log(key); log(values); log(rereduce); return sum(values); }" } ''' print query_temp_view('phone', view, '?group=true'); print ':::: let us look at the parm group.. if we set group=false : ' print query_temp_view('phone', view, '?group=false'); ############################################################################ print ':::: let us get a list of unique os , just like SQL: SELECT DISTINCT(os) FROM phone' view = ''' { "map" : "function(doc){ emit(doc.os, null); }", "reduce" : "function(key, values, rereduce){ return null; }" } ''' print query_temp_view('phone', view, '?group=true'); ############################################################################ print ':::: let us get all phone sort by price || SELECT _id FROM phone SORT BY price' view = ''' { "map" : "function(doc){ emit(doc.price, doc._id); }", } ''' print query_temp_view('phone', view); ############################################################################ print ':::: let us get min price || SELECT min(price) FROM phone ' view = ''' { "map" : "function(doc){ emit('p', doc.price); }", "reduce" : "function(key, values, rereduce){ return Math.min.apply( Math, values); //http://labs.mudynamics.com/wp-content/uploads/2009/04/icouch.html 上 //computing min width/height ( js模拟)的例子在我这里不行 }" } ''' print query_temp_view('phone', view, '?group=true'); ############################################################################ print ':::: I hava to try emit a array like emit([a,b,c], value)' view = ''' { "map" : "function(doc){ emit(['p', 'min'], doc.price); emit(['p', 'max'], doc.price); }", "reduce" : "function(key, values, rereduce){ log('reduce called!!!!'); log(key); log(values); log(rereduce); return Math.min.apply( Math, values); }" } ''' print query_temp_view('phone', view, '?group=true&group-level=2'); print ':::: if gropu-level==1 ' print ':::: [p, min] and [p, max] will come together to a reduce fun, like this' print ':::: [[["p","max"],"BlackBerry-Bold"],[["p","max"],"HTC-Wildfire"],[["p","max"],"iPhone4"],[["p","max"],"Nokia-1661"],[["p","max"],"Nokia-5200"],[["p","max"],"Nokia-E63"],[["p","max"],"Samsung-Galaxy-S"],[["p","min"],"BlackBerry-Bold"],[["p","min"],"HTC-Wildfire"],[["p","min"],"iPhone4"],[["p","min"],"Nokia-1661"],[["p","min"],"Nokia-5200"],[["p","min"],"Nokia-E63"],[["p","min"],"Samsung-Galaxy-S"]]' print ':::: if gropu-level==2 ' print ':::: [p, min] and [p, max] will come separate.......... like this' print '[[["p","min"],"Samsung-Galaxy-S"],[["p","min"],"Nokia-E63"],[["p","min"],"Nokia-5200"],[["p","min"],"Nokia-1661"],[["p","min"],"iPhone4"],[["p","min"],"HTC-Wildfire"],[["p","min"],"BlackBerry-Bold"]]' #TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTODO ############################################################################ print ':::: let us retrive top N os' if __name__ == "__main__": test()