couchdb

1、简介

Apache CouchDB数据库,它类似于Redis,Cassandra和MongoDB,也是一个NoSQL数据库。 CouchDB将数据存储为非关系性的JSON文档。
couchdb内部通讯采用httpClient。

2、安装

https://www.w3cschool.cn/couchdb/couchdb_installation.html
1)远程访问需配置
修改/opt/couchdb/etc/下local.ini文件:
1.x版本在[httpd]节点下修改,2.x版本在[chttpd]节点下修改
port = 5984
bind_address = 0.0.0.0

2)设置couchdb用户名密码
修改/opt/couchdb/etc/下local.ini文件:
[admins]
用户名 = 密码

3、启动

在根目录下
/etc/init.d/couchdb start
/etc/init.d/couchdb stop
/etc/init.d/couchdb restart
开机自动启动:
chkconfig couchdb on

4、curl命令操作数据库

https://www.w3cschool.cn/couchdb/couchdb_curl_futon.html
http://www.cnblogs.com/oloroso/p/9767546.html

5、java语言操作数据库

** ektorp **

    public static void createDesign(PrintWriter out) throws MalformedURLException {
		// Default connector for testing
	    HttpClient httpClient = new StdHttpClient.Builder().url("http://username:password@ip:port").build();
//		HttpClient httpClient = new StdHttpClient.Builder().build();
	    CouchDbInstance dbInstance = new StdCouchDbInstance(httpClient);
		CouchDbConnector db = new StdCouchDbConnector("ektorp", dbInstance);
		db.createDatabaseIfNotExists();
		String viewString = "id_view";
		
		if (!db.contains(viewString)) {
			DesignDocument dd = new DesignDocument(viewString);
			String mapColor = "red; white";
			String mapSize = "big; small";
				
			DesignDocument.View view = new DesignDocument.View(mapColor);
			dd.addView("mapColor", view);
		
			view = new DesignDocument.View(mapSize);
			dd.addView("mapSize", view);
			
			db.create(dd);
			out.println("add view = " + view.toString());
		} else {
			DesignDocument designDoc;
			List<String> docIds = db.getAllDocIds();
			String docId = "";
			String docVersion = "";
			for (int i = 0; i < docIds.size(); i ++ ) {
				docId = docIds.get(i);
				out.println("get docId = " + docId);
				if (docId.equals(viewString)) {
					out.println("delete docId = " + docId);
//					docVersion = db.getCurrentRevision(docId);
					docVersion = db.getRevisions(docId).get(0).getRev();
//					designDoc = db.get(DesignDocument.class, docId);
//					db.delete(designDoc);	
					db.delete(docId, docVersion);
					out.println("delete view id = " + docId);
				}
			}
		}
	}

** lightcouch **

public class CouchdbByLightCouchTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out = resp.getWriter();
        resp.getWriter().println("CouchdbByLightCouchTest");
        try {
            callCouch(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public CouchDbProperties getProperties() {
        CouchDbProperties properties = new CouchDbProperties()
                .setUsername("username")
                .setPassword("password")
                .setDbName("lightcouch-db-load")
                .setCreateDbIfNotExist(true)
                .setProtocol("http")
                .setHost("ip")
                .setPort(5984)
                .setMaxConnections(MAX_CONNECTIONS);
        return properties;
    }

    private void callCouch(PrintWriter out) {
        dbClient = new CouchDbClient(getProperties());

        JsonObject jsonObject = null;
        Response resp = null;

        List<String> allDbs = dbClient.context().getAllDbs();
        out.println("allDbs = " + allDbs);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("_id", "map-id");
        map.put("title", "value");
        resp = dbClient.save(map);
        jsonObject = dbClient.find(JsonObject.class, resp.getId());
        dbClient.remove(resp.getId(), resp.getRev());

        JsonObject json = new JsonObject();
        json.addProperty("_id", "json-id");
        json.add("array", new JsonArray());
        resp = dbClient.save(json);
        jsonObject = dbClient.find(JsonObject.class, resp.getId());
        dbClient.remove(resp.getId(), resp.getRev());

//        String uri = dbClient.getBaseUri() + "_stats";
//        jsonObject = dbClient.findAny(JsonObject.class, uri);

        String jsonstr = "{\"title\":\"val\"}";
        jsonObject = dbClient.getGson().fromJson(jsonstr, JsonObject.class);
        resp = dbClient.save(jsonObject);
        dbClient.remove(resp.getId(), resp.getRev());

        // shutdown the client
        dbClient.shutdown();

    }
}

posted @ 2018-11-15 16:02  颜林小钦  阅读(1583)  评论(0编辑  收藏  举报