javacouchbasespring-data-couchbase

How to create Couchbase bucket via java API?


I am Using Spring data couchbase .

 package com.CouchbaseMine.config;

    import java.io.IOException;
    import java.net.URI;
    import java.util.Arrays;
    import java.util.LinkedList;
    import java.util.List;

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;

    import com.couchbase.client.CouchbaseClient;


    @Configuration
    @EnableAutoConfiguration
    public class CouchbaseMineCouchBaseConfig extends AbstractCouchbaseConfiguration {


        @Value("${couchbase.cluster.bucket}")
        private String bucketName;

        @Value("${couchbase.cluster.password}")
        private String password;

        @Value("${couchbase.cluster.ip}")
        private String ip;


        @Override
        protected String getBucketName() {

            List<URI> uris=new LinkedList<URI>();
            uris.add(URI.create("5x.xx.xxx.xx9"));
            CouchbaseClient client=null;
            try {
                System.err.println("-- > - > i am in ");
                client=new CouchbaseClient(uris,"default","");

            } catch (IOException e) {

                System.err.println("IOException connetion to couchbase:"+e.getMessage() );
                System.exit(1);
            }
            return this.bucketName;
        }

        @Override
        protected String getBucketPassword() {
            return this.password;
        }

        @Override
        protected List<String> bootstrapHosts() {
            // TODO Auto-generated method stub
            //return Collections.singletonList("54.89.127.249");
            return Arrays.asList(this.ip);
        }

    }

This is configuration class used for establish connection

Follow application properties file

server.port=3000
couchbase.cluster.ip 5x.xx.xxx.xx9
couchbase.cluster.bucket DHxxxar
couchbase.cluster.password 1221

Bottom line: I have created the bucket (Dhxxxar) manually in couchbase.But i need to automatically create the bucket(database) while i run my spring boot application.

So give me any suggestion regards the same . Thanks in advance


Solution

  • 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);
    

    More details: https://developer.couchbase.com/documentation/server/current/sdk/java/managing-clusters.html