在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
其他资料:
Fedora: http://library.linode.com/databases/couchdb/fedora-14
Ubuntu: http://www.oschina.net/question/5189_7729
使用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());
}
}
整理后编写如下:
@Configuration public class CouchDBTemplate { @Value("${fabric.couchdb.host}") private String couchDBHost; @Value("${fabric.couchdb.database}") private String databaseName; public CouchDbConnector getCouchDbConnector() throws MalformedURLException { HttpClient httpClient = new StdHttpClient.Builder().url(couchDBHost).build(); CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient); CouchDbConnector db = new StdCouchDbConnector(databaseName, dbInstance); db.createDatabaseIfNotExists(); return db; } }
@Service
public class CouchDBServiceImpl implements CouchDBService {
@Autowired
private CouchDBTemplate couchDBTemplate;
@Override
public void test() throws MalformedURLException {
CouchDbConnector db = couchDBTemplate.getCouchDbConnector();
Sofa sofa = db.get(Sofa.class, "0x789456123");
System.out.println(JSONObject.toJSON(sofa));
}
@Override
public Long getEvidenceSum() throws MalformedURLException {
CouchDbConnector db = couchDBTemplate.getCouchDbConnector();
Long cc =db.getDbInfo().getDocCount();
return cc;
}
}