python - couchbase python 客户端中是否缺少 flush 方法?

标签 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 中尝试了智能感知。

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

最佳答案

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 类的描述(来自源):

An administrative connection to a Couchbase cluster.

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

关于python - couchbase python 客户端中是否缺少 flush 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22875990/

相关文章:

Python脚本需要简单修改才能在文件中打印

python - 标准格式 matplotlib -- 将 e 更改为\times 10

python - 通过 DictWriter (Python) 将字典写入 CSV 文件

php - 在 Windows 上的 XAMPP 上为 PHP 安装 couchbase 客户端 SDK

c# - 获取同步网关心跳

rx-java - 使用响应式(Reactive)编程将 couchbase 文档从一个存储桶复制到另一个存储桶时出现 OOM 问题

python - 使用查询选项为 CouchDB 编码 curl-ready URL

python - 使用 "re.findall"拆分字符串时出错