简体   繁体   English

如何从Python CouchDB使用Iterview函数

[英]How to use iterview function from python couchdb

I have been working with couchdb module in python to meet some projects needs. 我一直在使用python中的couchdb模块来满足某些项目需求。 I was happily using view method from couchdb to retrieve result sets from my database until recently. 直到最近,我一直在快乐地使用来自ouchdb的查看方法从数据库中检索结果集。

for row in db.view(mapping_function):
    print row.key

However lately I have been needing to work with databases a lot bigger in size than before (~ 15-20 Gb). 但是最近我一直在使用比以前更大的数据库(〜15-20 Gb)。 This is when I ran into an unfortunate issue. 这是我遇到一个不幸的问题。

db.view() method loads all rows in memory before you can do anything with it. db.view()方法将加载内存中的所有行,然后才能执行任何操作。 This is not an issue with small databases but a big problem with large databases. 这不是小型数据库的问题,而是大型数据库的大问题。

That is when I came across iterview function. 那是我遇到iterview函数的时候。 This looks promising but I couldn't find a example usage of it. 这看起来很有希望,但是我找不到它的示例用法。 Can someone share or point me to example usage of iteview function in python-couchdb 有人可以分享或指出我在python-couchdb中使用iteview函数的示例吗

Thanks - A 谢谢

Doing this is almost working for me: 这样做几乎对我有用:

import couchdb.client
server = couchdb.client.Server()
db = server['db_name']
for row in db.iterview('my_view', 10, group=True):
    print row.key + ': ' + row.value

I say it almost works because it does return all of the data and all the rows are printed. 我说这几乎可行,因为它确实返回了所有数据,并且所有行都被打印了。 However, at the end of the batch, it throws a KeyError exception inside couchdb/client.py (line 884) in iterview 但是,在批处理结束时,它将在iterview中的ouchdb / client.py(第884行)内引发KeyError异常。

This worked for me. 这对我有用。 You need to add include_docs=True to the iterview call, and then you will get a doc attribute on each row which can be passed to the database delete method: 您需要在iterview调用中添加include_docs=True ,然后将在每行上获得一个doc属性,该属性可以传递给数据库delete方法:

import couchdb
server = couchdb.Server("http://127.0.0.1:5984")
db = server['your_view']
for row in db.iterview('your_view/your_view', 10, include_docs=True):
    # print(type(row))
    # print(type(row.doc))
    # print(dir(row))
    # print(row.id)
    # print(row.keys())
    db.delete(row.doc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM