3

I did not find the flush button for bucket in couchbase admin UI at port 8091 .May be because of this http://www.couchbase.com/issues/browse/MB-5351 .

Then I saw this How to delete all items in the bucket? so I wanted to do flush in python client .

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 "

Here client i did not find a method to flush . I tried intellisense in IDE .

Please guide me to flush the bucket via python client .

Community
  • 1
  • 1
Harish Kayarohanam
  • 3,886
  • 4
  • 31
  • 55
  • In the UI there should be an option to enable flushing a bucket. Can you make sure that this radio box is checked. – mikewied Apr 05 '14 at 18:49
  • The 'Flush' button is in the *edit* dialog of a button; but you need to enable the flush option (also in the *edit* dialog) before it appears. – DaveR Apr 05 '14 at 18:50
  • Ya I am able to see the flush button . Thanks . But why there is no way to flush from couchbase python client. – Harish Kayarohanam Apr 06 '14 at 09:40
  • So looking at the .NET Couchbase SDK, they have a flushbucket() - and the Python SDK doesn't? That can't be right... – John C Jan 06 '15 at 18:07

1 Answers1

1

It appears that the couchbase python SDK does not currently provide a flush() method. But since the flush method is provided through the couchbase REST API you may use it to flush your bucket.

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

The python SDK provides an Admin object which you can use to perform your administrator tasks using REST using its http_request method. source code: https://github.com/couchbase/couchbase-python-client/blob/master/couchbase/admin.py

description of the Admin class (from source):

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 method description (from source):

    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.

Example:

First make sure that the Flush option is enabled for your bucket.

I created a test bucket named "default" for the example and created 2 documents within the bucket.

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']

result:

Successfully made an admin connection 
# of items before flush:  2
# of items after flush:  0
KorreyD
  • 1,274
  • 8
  • 15
  • That might work, but what about code that doesn't have access to the Administator password? (Which is why I was looking for a way to do it using the Python SDK in the first place). – John C Jan 12 '15 at 14:43
  • 2
    seems they have an open issue...since last year. https://issues.couchbase.com/browse/PYCBC-214 – Unknown Jan 13 '15 at 14:43
  • @JohnC unfortunately I stopped looking once i came to the solution above. After reading your comment, I did try quickly with username=None and password=None and was still able to connect and flush the bucket on my default setup (Though I have not taken the time to figure out why it allowed me to do that... It may be something you can look into further though). – KorreyD Jan 13 '15 at 19:15