couchbase-python-client and _all_docs
Hi, I need to get a list of all documents stored in the database. How can I do this with couchbase-python-client?
I found the class CouchdbReader, but I think it does not work. The construction bucket.view ('_all_docs') gives an error, sо I do the following:
class Cdb(object): def __init__(self, *args, **kwargs): self.couchbase = Couchbase(':'.join([COUCHBASE['ip'],COUCHBASE['port']]), COUCHBASE["username"],COUCHBASE['password']) self.bucket = self.couchbase[COUCHBASE["bucket"]] self.limit=10 self.rest = RestConnection(COUCHBASE) self.base_url = "http://{0}:{1}".format(COUCHBASE['ip'], 8092) self.items = self.get_data() def get_data(self, startkey=None): api = '/{0}/{1}?'.format(COUCHBASE["bucket"], '_all_docs') if self.limit: api+='limit=%s'%str(self.limit+1) if startkey: api+='&startkey="%s"'%startkey headers = self.rest._create_capi_headers() status, content = self.rest._http_request(api, headers=headers,base=self.base_url) items = json.loads(content) return items.get('rows',[]) def __iter__(self): return self def next(self): if len(self.items) < 1: raise StopIteration() elif len(self.items) == 1: self.items = self.get_data(startkey=self.items[0]['key']) data = self.items.pop(0) else: data = self.items.pop(0) return data
Tell me, please, what are other ways to solve this problem?
The the migration/dump script (couchbase-migrator.py) shipped with couchbase-python-client also fails because of this:
couchbase-migrator.py --source=couchbase://user:pw@localhost:8091/mybucket --destination=dir://dump
[2013-04-19 14:36:08,470] - [rest_client] [140735186164064] - ERROR - http://127.0.0.1:8092/mybucket/_design/_all_docs/_view/None?limit=101&st... error 404 reason: not_found {"error":"not_found","reason":"Design document _design/_all_docs not found"}
Traceback (most recent call last):
File "/Users/ville/.virtualenvs/couchbase/lib/python2.7/site-packages/couchbase/utils/couchbase-migrator.py", line 132, in
reader = couchbase.migrator.reader(config.source)
File "/Users/ville/.virtualenvs/couchbase/lib/python2.7/site-packages/couchbase/migrator/__init__.py", line 40, in reader
return CouchbaseReader(fp)
File "/Users/ville/.virtualenvs/couchbase/lib/python2.7/site-packages/couchbase/migrator/migrator_couchbase.py", line 50, in __init__
include_docs=True)
File "/Users/ville/.virtualenvs/couchbase/lib/python2.7/site-packages/couchbase/client.py", line 332, in view
params, limit)
File "/Users/ville/.virtualenvs/couchbase/lib/python2.7/site-packages/couchbase/rest_client.py", line 266, in view_results
+ repr(status) + "\n" + content)
Exception: unable to obtain view results for mybucket/_design/_all_docs/_view/None?limit=101&stale=false&reduce=false&include_docs=true
False
{"error":"not_found","reason":"Design document _design/_all_docs not found"}
Isn't this a bug in the tools supplied with couchbase-python-client that it relies on this unsupported API?
As Tug says, the recommend way it to use a View.
The current SDK has plenty of problems. All energy is currently spent on a new SDK which is a complete rewrite. Best for now is to wait for the new SDK to arrive.
If you've any specific problems, please post them, so we can make sure to get those features work in the new SDK as soon as possible.
Cheers,
Volker
Hello,
The all_docs view is an internal view that is not documented nor supported.
So if you want to do this you will need to create your own view and emit the field you want.
This is probably the only use case where emitting the doc ID is interesting:
function (doc, meta) { emit(meta.id); }For which type of use case do you need this?
Regards
Tug
@tgrall