I was on the CouchDB IRC channel last night getting some guidance on using externals in the latest couch (I got the source from svn last night). I thought I’d post my example code for you.
Here is the local.ini file
; CouchDB Configuration Settings
; Custom settings should be made in this file. They will override settings
; in default.ini, but unlike changes made to default.ini, this file won't be
; overwritten on server upgrade.
[couchdb]
;max_document_size = 4294967296 ; bytes
[httpd]
;port = 5984
bind_address = 0.0.0.0
[log]
;level = info
[httpd_db_handlers]
_test = {couch_httpd_external, handle_external_req, <<"anystring">>}
[external]
anystring = /home/tim/git/couchish/couchish/external.py
[update_notification]
;unique notifier name=/home/tim/git/couchish/couchish/notifier.py
I’ve used anystring here to show that it is just a reference between the http handler and the external
The script is taken from the CouchDB wiki (with a couple of modifications) and basically echos queries back to you. Don’t forget to make sure it’s executable by the couchdb user (I had a problem where the couchdb user’s home directory didn’t exist and hence the simplejson egg couldn’t unpack). su - to the couchdb user and run the script to double check.
#! /usr/bin/env python
import sys
import simplejson
_logfile = file('/tmp/external.log', 'a')
def log(msg):
_logfile.write('%s\n'%(msg,))
_logfile.flush()
def requests():
# `for line in sys.stdin` won't work here
line = sys.stdin.readline()
while line:
data = simplejson.loads(line)
log('data: %s'%data)
yield data
line = sys.stdin.readline()
def respond(code=200, data={}, headers={}):
sys.stdout.write("%s\n" % simplejson.dumps({"code": code, "json": data, "headers": headers}))
sys.stdout.flush()
def main():
for req in requests():
respond(data={"qs": req["query"]})
if __name__ == "__main__":
main()
test this out by using curl to send a query to the /mydbname/_ test url
curl http://localhost:5984/mydbname/_test?query=foo
Refer to http://wiki.apache.org/couchdb/ExternalProcesses for more info about the data going in and coming out..

0 comments:
Post a Comment