A newer version of this documentation is available.

View Latest

Provisioning Cluster Resources

    +
    Provisioning cluster resources is managed at the collection or bucket level, depending upon the service affected. Common use cases are outlined here, more recherché use cases are covered in the API docs.

    Unresolved include directive in modules/howtos/pages/provisioning-cluster-resources.adoc - include::6.5@sdk:shared:partial$flush-info-pars.adoc[]

    The Java SDK also comes with some convenience functionality for common Couchbase management requests.

    Management operations in the SDK may be performed through several interfaces depending on the object:

    • BucketManager — Cluster.Buckets()

    • UserManager — Cluster.Users()

    • QueryIndexManager — Cluster.QueryIndexes()

    • AnalyticsIndexManager — Cluster.AnalyticsIndexes()

    • SearchIndexManager — Cluster.SearchIndexes()

    • CollectionManager — Bucket.Collections()

    • ViewIndexManager — Bucket.ViewIndexes().

    When using a Couchbase version earlier than 6.5, you must create a valid Bucket connection using cluster.Bucket(name) before you can use cluster level managers.

    Creating and Removing Buckets

    The ClusterManager interface may be used to create and delete buckets from the Couchbase cluster. It is instantiated through the Cluster.Buckets() method. Refer to the API Bucket documentation — and to its Async counterpart — for further details.

    The CreateBucketSettings and BucketSettings structs are used for creating and updating buckets, BucketSettings is also used for exposing information about existing buckets.

    Unresolved include directive in modules/howtos/pages/provisioning-cluster-resources.adoc - include::6.5@sdk:shared:partial$flush-info-pars.adoc[]

    Flushing Buckets

    Unresolved include directive in modules/howtos/pages/provisioning-cluster-resources.adoc - include::6.5@sdk:shared:partial$flush-info-pars.adoc[]

    You can flush a bucket in the SDK by using the FlushBucketOptions — see the API docs.

    Collection Management

    Developer Preview

    The CollectionManager interface may be used to create and delete scopes and collections from the Couchbase cluster. It is instantiated through the Bucket.collections() method. Refer to the CollectionManager API documentation — and to its Async counterpart — for further details.

    Cluster cluster = Cluster.connect("localhost", "scopeAdmin", "password");
    Bucket bucket = cluster.bucket("travel-sample");
    CollectionManager collectionMgr = bucket.collections();

    You can create a scope:

    try {
      collectionMgr.createScope("example-scope");
    }
    catch (ScopeExistsException e) {
      System.out.println("Scope already exists");
    }

    You can then create a collection within that scope:

    CollectionSpec spec = CollectionSpec.create("example-collection", "example-scope");
    
    try {
      collectionMgr.createCollection(spec);
    }
    catch (CollectionExistsException e) {
      System.out.println("Collection already exists");
    }
    catch (ScopeNotFoundException e) {
      System.out.println("The specified parent scope doesn't exist");
    }

    Finally, you can drop unneeded collections and scopes:

    try {
      collectionMgr.dropCollection(spec);
    }
    catch (CollectionNotFoundException e) {
      System.out.println("The specified collection doesn't exist");
    }
    catch (ScopeNotFoundException e) {
      System.out.println("The specified parent scope doesn't exist");
    }
    
    try {
      collectionMgr.dropScope("example-scope");
    }
    catch (ScopeNotFoundException e) {
      System.out.println("The specified scope doesn't exist");
    }

    Note that the most minimal permissions to create and drop a Scope or Collection is Manage Scopes along with Data Reader

    You can create users with the appropriate RBAC programmatically:

    users.upsertUser(
      new User("scopeAdmin")
      .password("password")
      .displayName("Manage Scopes [travel-sample:*]")
      .roles(
        new Role("scope_admin", "travel-sample", "*", "*"),
        new Role("data_reader", "travel-sample", "*", "*")));

    Index Management

    In general,you will rarely need to work with Index Managers from the SDK. For those occasions when you do, please see the relevant API docs:

    View Management

    Unresolved include directive in modules/howtos/pages/provisioning-cluster-resources.adoc - include::6.5@sdk:shared:partial$flush-info-pars.adoc[]

    In the SDK, design documents are represented by the DesignDocument and View structs. All operations on design documents are performed on the ViewIndexManager instance: