CouchDB客户端开发—Java版

Posted yuluoxingkong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CouchDB客户端开发—Java版相关的知识,希望对你有一定的参考价值。

在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;
    }
    
    
}
public class Sofa {
    
    @JsonProperty(value = "_id")
    private  String id;
    
    @JsonProperty(value = "_rev")
    private  String rev;
    
    private  String address;
    
    private  String createTime;
    
    private  String hash;
    
    private  String name;

    private  String recordNumber;
    
    private  String source;

    private  String user;
    
    @JsonProperty(value = "~version")
    private  String version;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getRev() {
        return rev;
    }

    public void setRev(String rev) {
        this.rev = rev;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    
    
    public String getRecordNumber() {
        return recordNumber;
    }

    public void setRecordNumber(String recordNumber) {
        this.recordNumber = recordNumber;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
    
    
}

 





参考 https://www.xuebuyuan.com/2222739.html

以上是关于CouchDB客户端开发—Java版的主要内容,如果未能解决你的问题,请参考以下文章

使用 Autobahn WebSocket 库将 sendMessage 用于 CouchDB 更改

couchDB designDoc 开发工作流程和版本控制

如何从 couchdb 运行服务器端代码?

Docker部署CouchDB

[vscode]--HTML代码片段(基础版,reactvuejquery)

创建片段而不从 java 代码实例化它