在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()); } }
CouchDB4J:
package com.yuxipacific; import java.util.List; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import com.fourspaces.couchdb.Database; import com.fourspaces.couchdb.Document; import com.fourspaces.couchdb.Session; import com.fourspaces.couchdb.ViewResults; public class CouchDB4J { public static void main(String[] args) { Session session = new Session("192.168.11.124",5984); Database database = session.getDatabase("mydatabase"); ViewResults vr = database.getAllDocuments(); System.out.println(vr.toString()); JSONArray jsonData = (JSONArray)vr.get("rows"); for (Object object : jsonData) { JSONObject jsonObject = (JSONObject) object; System.out.println(jsonObject.get("key")); } List<Document> documents = vr.getResults(); for (Document document : documents) { System.out.println(document.toString()); } } }
http://wiki.apache.org/couchdb/Getting_started_with_Java
Ektorp : https://github.com/helun/Ektorp
couchdb4j: http://code.google.com/p/couchdb4j/downloads/list
Relaxed Java Persistence with Ektorp and CouchDB
http://ektorp.org/tutorial.html
http://ektorp.org/reference_documentation.html