Cloud request with python

There may be an obvious answer to this question, but I cannot find it anywhere: what is the best way to query couchdb databases stored on servers with a cloud platform? I am trying to use temporary views as well as couchdb.py instructions:

>>> db['johndoe'] = dict(type='Person', name='John Doe') >>> db['maryjane'] = dict(type='Person', name='Mary Jane') >>> db['gotham'] = dict(type='City', name='Gotham City') >>> map_fun = '''function(doc) { ... if (doc.type == 'Person') ... emit(doc.name, null); ... }''' >>> for row in db.query(map_fun): ... print row.key John Doe Mary Jane 

While this works on locally hosted databases, CloudAnt returns an error:

 couchdb.http.ServerError: (403, ('forbidden', 'temp views are disabled on Cloudant')) 

I read the cloud tutorial on demand, but the proposed query syntax seems awkward, and it's not clear how to use it in python! Is there an easy way around this?

+4
source share
4 answers

The reason Cloudant prohibits viewing paces is because they are not scalable. You will need to create a project document with specific views in it. Here is a link to what a project document with views defined on it is:

http://max.ic.ht/_utils/document.html?action/_design/action

I'm not sure how to do this on couchdb.py, but you can try a different python library. Here is a link to the section on creating views in couchquery

http://mikeal.github.com/couchquery/#creating-views

+1
source

This is how I add a post with python.

 import requests import json doc = { 'username':'kerrie', 'high_score':550, 'level':3 } auth = ('username', 'password') headers = {'Content-type': 'application/json'} post_url = "https://account.cloudant.com/database/kerrie".format(auth[0]) r = requests.put(post_url, auth=auth, headers=headers, data=json.dumps(doc)) print json.dumps(r.json(), indent=1) 

This is how I request 10 entries in Python.

 import requests import json auth = ('username', 'password') get_url = "https://account.cloudant.com/database/_all_docs?limit=10".format(auth[0]) r = requests.get(get_url, auth=auth) print json.dumps(r.json(), indent=1) 
+2
source

You should probably use couchdbkit . This makes it easy to customize your views. I don’t think you can use temporary views in Cloudant anymore.

0
source

Just noting that Cloudant now has the official python library, https://github.com/cloudant/python-cloudant .

0
source

All Articles