How to create a couchbase bucket through code in spring boot

Viewed 195

I am new to springboot application development. I tried and was able to develop a simple springboot app to connect to a couchbase server and perform crud operations on its buckets. Now, my requirement is to not connect to an already created bucket in the couchbase server but to create a new bucket and store some documents in it through my spring boot app. I am using annotation based configuration to specify the connection configuration by extending AbstractCouchbase repository and implementing its methods.

2 Answers

Can you try this :

Cluster cluster = CouchbaseCluster.create("127.0.0.1");
ClusterManager clusterManager = cluster.clusterManager("Administrator", "12345");
BucketSettings bucketSettings = new DefaultBucketSettings.Builder()
        .type(BucketType.COUCHBASE)
        .name("hello")
        .quota(120)
        .build();

clusterManager.insertBucket(bucketSettings);

thank you for asking!

Here'se an example from our java spring boot tutorial:

    @Bean
    public Bucket getCouchbaseBucket(Cluster cluster){

        //Creates the bucket if it does not exist yet
        if( !cluster.buckets().getAllBuckets().containsKey(dbProp.getBucketName())) {
            cluster.buckets().createBucket(
                BucketSettings.create(dbProp.getBucketName())
                    .bucketType(BucketType.COUCHBASE)
                    .minimumDurabilityLevel(DurabilityLevel.NONE)
                    .ramQuotaMB(128));
        }
        return cluster.bucket(dbProp.getBucketName());
    }
Related