Wednesday, February 27, 2008

python-couchdb rocks

I've been playing around with couchdb using python-couchdb on ubuntu 7.10. It's in debian sid unstable so it can be installed trivially with apt-get or synaptic. You can also install it the old-fashioned way by following these instructions.

Couchdb is explained here. I was searching for a system that was close enough to the VIA repository that we used at MAYA. Couchdb is that system.

Here is an interesting article on couchdb performance and a response from Damien Katz.

Couchdb has a well-designed web interface that is accessible at http://server/_utils/index.html.

I use postgres for relational stuff, but I find trying to keep relational and the front end code in synch tedious. Couchdb seems to solve that for me without paying too heavy a price. At the moment, the major missing piece appears to be authentication. That is, there isn't any. That'll be a show-stopper for a lot of projects, but I still find the usability of couchdb compelling.

Here's a short sample in python that I worked up from the documentation in python-couchdb that shows how to create a database, delete a database, create a document with a python class-based schema, store the documents, and retrieve the documents using the db directly, and the query interface as well. HTH. Thanks to Nick Smallwood for the query code.

import couchdb
from couchdb.client import Server
from couchdb.schema import Document, TextField, IntegerField, DateTimeField
import datetime
import time

print '*' * 80
print 'begin'
print '*' * 80

# the couchdb server we're talking to
server = Server('http://192.168.1.30:5984/')
print 'established database connection'

# create a database, if it already exists, delete and recreate it
try:
db = server.create('animals')
print 'database created'
except:
del server['animals']
db = server.create('animals')
print 'database deleted and created'

print '*' * 80

# create a document class
class Animal(Document):
name = TextField()
age = IntegerField()
added = DateTimeField(default=datetime.datetime.now())

# our input data
animals = [{'name': 'keiko', 'age': 5}, {'name': 'goose', 'age': 11}]

# store the data in the database
for each in animals:
animal = Animal(name=each['name'], age=each['age'])
animal.store(db)
print 'animals stored in database'

print '*' * 80

# read the data from the database
print 'reading all animals from database'
print len(db), 'animals retrieved from database'
print 'name', 'age', 'added'
for uuid in db:
animal = Animal.load(db, uuid)
print animal.name, animal.age, animal.added

print '*' * 80

# query the database
print 'querying the database'
code = '''function(doc) { if (doc.name=="goose"){ map([doc.name], doc); }}'''
results = db.query(code)
print len(results), 'animals retrieved from database'
print 'name', 'age', 'added'
for res in results:
print res.value['name'], res.value['age'], res.value['added']
print '*' * 80
print 'the end'
print '*' * 80

3 Comments:

Blogger ultradaniel said...

Thanks for this post! It's exactly what I needed to start :D.

BTW... do you know where I could find more query examples?

5:10 PM  
Blogger Craig said...

> I worked up from the documentation in python-couchdb
Where is the documentation?
Sorry if it's a stupid question, but I can't find any. :/

I'm using CouchDB-0.5-py2.5.egg, which I got from http://pypi.python.org/pypi/CouchDB.

I was directed there from the google code page for CouchDB-python:
http://code.google.com/p/couchdb-python/

Cheers!

10:11 AM  
Blogger Rick Spencer said...

Making queries by passing in strings that are javascript functions is very interesting. It seems very straightforward, and javascript seems like the right language for that kind of thing.

4:46 PM  

Post a Comment

Links to this post:

Create a Link

<< Home