当前位置: 首页 > 工具软件 > sofa-CouchDB > 使用案例 >

couchdb java 连接_CouchDB客户端开发—Java版

凌琦
2023-12-01

在Fedora上安装CouchDB:

yum update

yum install couchdb

修改/etc/couchdb下local.ini文件:

port = 5984

bind_address = 0.0.0.0

启动couchdb:

/etc/init.d/couchdb start

重启couchdb:

/etc/init.d/couchdb restart

关闭couchdb:

/etc/init.d/couchdb stop

开机自动启动:

chkconfig couchdb on

其他资料:

使用Java访问CouchDB:

Jar包:

/CouchDB/commons-io-2.1.jar

/CouchDB/commons-logging-1.1.1.jar

/CouchDB/httpclient-4.1.2.jar

/CouchDB/httpclient-cache-4.1.2.jar

/CouchDB/httpcore-4.1.2.jar

/CouchDB/jackson-core-asl-1.9.4.jar

/CouchDB/jackson-mapper-asl-1.9.4.jar

/CouchDB/org.ektorp-1.2.2.jar

/CouchDB/slf4j-api-1.6.1.jar

/CouchDB/spring-2.5.6.jar

测试代码:

package com.couchdb;

import java.net.MalformedURLException;

import org.ektorp.CouchDbConnector;

import org.ektorp.CouchDbInstance;

import org.ektorp.http.HttpClient;

import org.ektorp.http.StdHttpClient;

import org.ektorp.impl.StdCouchDbConnector;

import org.ektorp.impl.StdCouchDbInstance;

public class CouchDBTest {

public static void main(String[] args) throws MalformedURLException {

HttpClient httpClient = new StdHttpClient.Builder().url("http://192.168.11.124:5984").build();

CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);

CouchDbConnector db = new StdCouchDbConnector("mydatabase", dbInstance);

db.createDatabaseIfNotExists();

Sofa sofa = db.get(Sofa.class, "id_1");

if(sofa != null) db.delete(sofa);

sofa = new Sofa();

sofa.setId("id_1");

sofa.setColor("red");

db.create(sofa);

Sofa tmpSofa = db.get(Sofa.class, "id_1");

System.out.println(tmpSofa.getRevision() + " : " + tmpSofa.getColor());

tmpSofa.setColor("blue");

db.update(tmpSofa);

tmpSofa = db.get(Sofa.class, "id_1");

System.out.println(tmpSofa.getRevision() + " : " + tmpSofa.getColor());

}

}

整理后编写如下:

@Configurationpublic classCouchDBTemplate {

@Value("${fabric.couchdb.host}")privateString couchDBHost;

@Value("${fabric.couchdb.database}")privateString databaseName;public CouchDbConnector getCouchDbConnector() throwsMalformedURLException {

HttpClient httpClient= newStdHttpClient.Builder().url(couchDBHost).build();

CouchDbInstance dbInstance= newStdCouchDbInstance(httpClient);

CouchDbConnector db= newStdCouchDbConnector(databaseName, dbInstance);

db.createDatabaseIfNotExists();returndb;

}

}

@Servicepublic class CouchDBServiceImpl implementsCouchDBService {

@AutowiredprivateCouchDBTemplate couchDBTemplate;

@Overridepublic void test() throwsMalformedURLException {

CouchDbConnector db=couchDBTemplate.getCouchDbConnector();

Sofa sofa= db.get(Sofa.class, "0x789456123");

System.out.println(JSONObject.toJSON(sofa));

}

@Overridepublic Long getEvidenceSum() throwsMalformedURLException {

CouchDbConnector db=couchDBTemplate.getCouchDbConnector();

Long cc=db.getDbInfo().getDocCount();returncc;

}

}

public classSofa {

@JsonProperty(value= "_id")privateString id;

@JsonProperty(value= "_rev")privateString rev;privateString address;privateString createTime;privateString hash;privateString name;privateString recordNumber;privateString source;privateString user;

@JsonProperty(value= "~version")privateString version;publicString getId() {returnid;

}public voidsetId(String id) {this.id =id;

}publicString getRev() {returnrev;

}public voidsetRev(String rev) {this.rev =rev;

}publicString getAddress() {returnaddress;

}public voidsetAddress(String address) {this.address =address;

}publicString getCreateTime() {returncreateTime;

}public voidsetCreateTime(String createTime) {this.createTime =createTime;

}publicString getHash() {returnhash;

}public voidsetHash(String hash) {this.hash =hash;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getRecordNumber() {returnrecordNumber;

}public voidsetRecordNumber(String recordNumber) {this.recordNumber =recordNumber;

}publicString getSource() {returnsource;

}public voidsetSource(String source) {this.source =source;

}publicString getUser() {returnuser;

}public voidsetUser(String user) {this.user =user;

}publicString getVersion() {returnversion;

}public voidsetVersion(String version) {this.version =version;

}

}

 类似资料: