user4563977
user4563977

Reputation:

Fetching data from a view written in CouchDB-Futon using Python

I have a database with around 2 million documents. This database contains a lot of duplicate documents. I wrote a map reduce function in CouchDB-Futon, which removes the duplicates. Now I want to get the values of this view in my python script.

import cloudant

account = cloudant.Account('Cricket-Harvestor')
db = account.database('Cricket-Analysis')
# view = Over here I want to reference the view I created in CouchDB futon.
for doc in view:
  # and so does this!
  print doc

Could someone please guide me over here? I have also referred below url:

https://pythonhosted.org/CouchDB/mapping.html

This uses couchdb-python library. I am creating and storing documents using the same library. But it seems to not serve purpose in getting data out of multiple documents from a view, which was created on CouchDB-Futon.

Upvotes: 1

Views: 1560

Answers (1)

Chris Snow
Chris Snow

Reputation: 24588

Your example code is using the Cloudant Python library.

Here is a similar example of querying a view on the Cloudant education account's animaldb database:

import cloudant
account = cloudant.Account('education')
db = account.database('animaldb')

doc = db.design('views101')  
view = doc.view('latin_name_jssum?reduce=false')

for doc in view:
   print doc

This outputs:

{u'value': 19, u'id': u'kookaburra', u'key': u'Dacelo novaeguineae'}
{u'value': 19, u'id': u'snipe', u'key': u'Gallinago gallinago'}
{u'value': 10, u'id': u'llama', u'key': u'Lama glama'}
{u'value': 11, u'id': u'badger', u'key': u'Meles meles'}
{u'value': 16, u'id': u'aardvark', u'key': u'Orycteropus afer'}

You can see the raw json response from the view at this url using curl or with your browser.

Here is the curl version:

snowch$ curl https://education.cloudant.com/animaldb/_design/views101/_view/latin_name_jssum?reduce=false
{"total_rows":5,"offset":0,"rows":[
   {"id":"kookaburra","key":"Dacelo novaeguineae","value":19},
   {"id":"snipe","key":"Gallinago gallinago","value":19},
   {"id":"llama","key":"Lama glama","value":10},
   {"id":"badger","key":"Meles meles","value":11},
   {"id":"aardvark","key":"Orycteropus afer","value":16}
]}

Upvotes: 1

Related Questions