How to create a bucket using Java SDK 3.1

I am trying to create a couchbase bucket using Java SDK 3.1. In the documentation(Java 2,7) there are examples to create bucket using the ClusterManager and BucketSettings.
Below is the way for creating bucket with java sdk 2.7:

Cluster cluster = CouchbaseCluster.create("");
ClusterManager clusterManager = cluster.clusterManager("Administrator", "123456");
BucketSettings bucketSettings = new DefaultBucketSettings.Builder()
    .quota(120) // megabytes


But after switching to Java SDK 3.1, I know ClusterManager.insertBucket has been replaced by BucketManager.createBucket. I am unsure as to how to use the createBucket method. I tried the below code and it gave me an error:

 Cluster cluster = null;
 cluster = Cluster.connect("", username, password); 
 Bucket bucket = cluster.bucket(bucketName);
 BucketManager manager = cluster.buckets();
 String configJson = "{\"name\" : \"new_bucket\" , \"controllers\": {\"flush\":\"false\"}"
                         + ",\"quota\":" + "{\"rawRAM\":" + 120 + "}"
                         + ",\"replicaNumber\":" + 0
                         + ",\"replicaIndex\":\"false\""
                         + ",\"bucketType\":\"membase\""
                     + ",\"conflictResolutionType\":\"seqno\""
                     + ",\"evictionPolicy\":\"valueOnly\""
                     + "}";
 BucketSettings bucketSettings = new ObjectMapper().readerFor(BucketSettings.class).readValue(configJson);

Error: Cannot construct instance of, problem: java.lang.NullPointerException``

It would be great if someone could help me with creating bucket through Java SDK 3.1


Hi Shailey,

No need to mess around with JSON. Here’s what that code from 2.7 would look like in SDK 3:



Hi David,

Thanks that worked when I pass it as you mentioned. But I am trying to create a program where I read the configurations from a JSON file. Also in the JSON, I was trying to pass Couchbase as the bucketType it gives: Cannot deserialize value of type from String “COUCHBASE”: not one of the values accepted for Enum class: [ephemeral, membase, memcached]. SO is it not possible to create a Couchbase bucket type using JSON configuration ?

I tried the below and it is still giving me a null pointer exception:

String configJson = "{\"name\":\"new_bucket\" , \"controllers\":{\"flush\":\"false\"}"
                        + ",\"quota\":" + "{\"ramQuota\":" + 120 + "}"
                                + ",\"replicaNumber\":" + 0
                                + ",\"replicaIndex\":\"false\""
                                + ",\"bucketType\":\"memcached\""
                            + "}";

BucketSettings bucketSettings = new ObjectMapper().readerFor(BucketSettings.class).readValue(configJson);             

The Error that i get is: Cannot construct instance of, problem: java.lang.NullPointerException
at [Source: (String)"{“name”:“new_bucket” , “controllers”:{“flush”:“false”},“quota”:{“ramQuota”:120},“replicaNumber”:0,“replicaIndex”:“false”,“bucketType”:“memcached”}"; line: 1, column: 146]


Hi Shaily,

You’ll need to change 2 things in that JSON document if you want to automatically map it to a BucketSettings object in SDK 3:

  1. ramQuota should be changed to rawRAM.
  2. The flush field in the controllers object should only be present when the bucket is flushable.

If you’re curious about the other field names, take a look at the source code.

As for the bucket type, memcached corresponds to BucketType.COUCHBASE. You can see this in the source code too.

If you can’t change the JSON, you’ll need to parse the JSON yourself and build the BucketSettings by hand. In fact, that’s the approach I would recommend anyway, since I don’t think creating a BucketSettings directly from JSON is officially supported. It works, but it’s not explicitly part of the SDK’s public API.