我正在使用Spring数据沙发库。
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);
}
}
这是用于建立连接的配置类
关注应用程序属性文件
server.port=3000
couchbase.cluster.ip 5x.xx.xxx.xx9
couchbase.cluster.bucket DHxxxar
couchbase.cluster.password 1221
底线:我已经在couchbase中手动创建了一个桶(Dhxxxar)。但是我需要在运行spring boot应用程序时自动创建存储桶(数据库)。
所以请给我任何关于同样的建议。提前致谢
答案 0 :(得分:1)
试试这个:
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);
更多细节: https://developer.couchbase.com/documentation/server/current/sdk/java/managing-clusters.html
答案 1 :(得分:1)
IgorekPotworek 的回答非常适合 Couchbase Java SDK 2.x 版。
对于 3.x 版本,代码看起来有点不同:
Cluster cluster = Cluster.connect("localhost", "Administrator", "password");
BucketManager bucketManager = cluster.buckets();
bucketManager.createBucket(
BucketSettings.create("bucketName")
.ramQuotaMB(100));