当前位置: 首页 > 工具软件 > 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());

}

}

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 documents = vr.getResults();

for (Document document : documents) {

System.out.println(document.toString());

}

}

}

https://github.com/helun/Ektorp

Relaxed Java Persistence with Ektorp and CouchDB

 类似资料: