couchbase python客户端缺少flush方法吗?

时间:2014-04-05 02:51:03

标签: python couchbase couchdb-python

我没有在端口8091的couchbase管理界面中找到存储桶的刷新按钮。可能是因为这个http://www.couchbase.com/issues/browse/MB-5351

然后我看到了这个How to delete all items in the bucket?所以我想在python客户端中进行刷新。

import sys
from couchbase import Couchbase
from couchbase.exceptions import CouchbaseError
try:
    client = Couchbase.connect(bucket='production',host='localhost',port=8091)
except CouchbaseError as e:
    print " Sorry , we could not create connection to bucket specified , due to " , e
else :
    print "Successfully made the connection to bucket "

这里的客户端我没有找到要刷新的方法。我在IDE中尝试过intellisense。

请指导我通过python客户端刷新存储桶。

1 个答案:

答案 0 :(得分:1)

看来couchbase python SDK目前没有提供flush()方法。 但由于flush方法是通过couchbase REST API提供的,因此您可以使用它来清空存储桶。

参考:Couchbase REST API,Buckets API - http://docs.couchbase.com/admin/admin/REST/rest-bucket-intro.html

python SDK提供了一个 Admin 对象,您可以使用其 http_request 方法使用REST来执行管理员任务。 源代码:https://github.com/couchbase/couchbase-python-client/blob/master/couchbase/admin.py

Admin类的描述(来自源代码):

  

与Couchbase群集的管理连接。

With this object, you can do things which affect the cluster, such as
modifying buckets, allocating nodes, or retrieving information about
the cluster.

This object should **not** be used to perform Key/Value operations. The
:class:`couchbase.bucket.Bucket` is used for that.

Admin.http_request方法说明(来自源代码):

    Perform an administrative HTTP request. This request is sent out to
    the administrative API interface (i.e. the "Management/REST API")
    of the cluster.

    See <LINK?> for a list of available comments.

    Note that this is a fairly low level function. This class will with
    time contain more and more wrapper methods for common tasks such
    as bucket creation or node allocation, and this method should
    mostly be used if a wrapper is not available.

示例:

首先确保为您的存储分区启用了Flush选项。

我为示例创建了一个名为“default”的测试存储桶,并在存储桶中创建了2个文档。

import sys
from couchbase import Couchbase
from couchbase.exceptions import CouchbaseError

#import Admin module
from couchbase.admin import Admin


#make an administrative connection using Admin object
try:
    admin = Admin(username='Administrator',password='password',host='localhost',port=8091)
except CouchbaseError as e:
    print " Sorry , we could not create admin connection , due to " , e
else :
    print "Successfully made an admin connection "


#retrieve bucket information for bucket named "default" 
#   "default" is just the name of the bucket I set up for trying this out
try:
    htres = admin.http_request("/pools/default/buckets/default")
except Exception as e:
    print "ERROR: ", e
    sys.exit()


#print the current number of items in the "default" bucket
#   "default" is just the name of the bucket I set up for trying this out
print "# of items before flush: ", htres.value['basicStats']['itemCount']


#flush bucket
try:
    #the bucket information request returned the path to the REST flush method for this bucket
    #   the flush method requires a POST request
    htres = admin.http_request(htres.value['controllers']['flush'],"POST")
except Exception as e:
    print "ERROR: ", e
    sys.exit()


#re-retrieve bucket information for bucket named "default" 
try:
    htres = admin.http_request("/pools/default/buckets/default")
except Exception as e:
    print "ERROR: ", e
    sys.exit()


#print the number of items in the "default" bucket (after flush)
print "# of items after flush: ", htres.value['basicStats']['itemCount']

结果:

Successfully made an admin connection 
# of items before flush:  2
# of items after flush:  0