How to create a bucket using Java SDK 3.1

Hello,
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("127.0.0.1");
ClusterManager clusterManager = cluster.clusterManager("Administrator", "123456");
BucketSettings bucketSettings = new DefaultBucketSettings.Builder()
    .type(BucketType.COUCHBASE)
    .name("hello")
    .password("")
    .quota(120) // megabytes
    .replicas(1)
    .indexReplicas(true)
    .enableFlush(true)
    .build();

clusterManager.updateBucket(bucketSettings);

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("127.0.0.1", 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);
 manager.createBucket(bucketSettings);

Error:

com.couchbase.client.core.deps.com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of com.couchbase.client.java.manager.bucket.BucketSettings, problem: java.lang.NullPointerException``

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

Thanks!

Hi Shailey,

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

cluster.buckets()
    .createBucket(BucketSettings.create("hello")
        .bucketType(BucketType.COUCHBASE)
        .ramQuotaMB(120)
        .numReplicas(1)
        .replicaIndexes(true)
        .flushEnabled(true));

Thanks,
David

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 com.couchbase.client.java.manager.bucket.BucketType 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);             
cluster.buckets().createBucket(bucketSettings);

The Error that i get is:
com.couchbase.client.core.deps.com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of com.couchbase.client.java.manager.bucket.BucketSettings, 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]

Thanks!

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.

Thanks,
David