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();

    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM