Couchbase Python 客户端中缺少 flush 方法吗?

3 投票
1 回答
905 浏览
提问于 2025-04-18 01:26

我在Couchbase的管理界面(8091端口)找不到清空桶的按钮。可能是因为这个问题

然后我看到这个如何删除桶中的所有项目?,所以我想在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里试了下智能提示。

请指导我如何通过Python客户端清空桶。

1 个回答

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对象,你可以用它通过REST来执行管理任务,使用它的http_request方法。

源代码链接: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”的测试桶,并在这个桶里创建了两个文档。

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

撰写回答