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

    }
}

转载于:https://www.cnblogs.com/linshuqin/p/9964128.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值