How to connect CouchDB with Java

时光怂恿深爱的人放手 提交于 2019-12-25 07:02:05

问题


I'm new about NOSQL. I use couchDB and ektrop Java API. I tried these code but it gives HTTP 405 error.

protected CouchDbInstance _db;
{       
String dbname = "my_database";
try {
//creates a database with the specified name
CouchDbConnector dbc = _db.createConnector(dbname, true);

//create a simple doc to place into your new database
Map<String, Object> doc = new HashMap<String, Object>();
doc.put("_id", UUID.randomUUID().toString());
doc.put("season", "summer");
doc.put("climate", "arid");
dbc.create(doc);

} catch (Exception e) {

}

Examples on the internet are very complex for me, so I didn't understand anything and i did not find any tutorial, so i have two questions.
-How can i connect db ?
-How can i add/delete/update documents operations ? If you give me examples codes, i will be really happy. Also you can suggest good tutorial. Thanks in advance.


回答1:


I am also new to CouchDB/NoSQL. But I am answering my best ignore if it not helps to you.

  1. It seems you are not even opening the session by passing user login credentials.
  2. Also you are directly trying to put Map object into DB create.
Session studentDbSession = new Session("localhost",5984);
Database studentCouchDb = studentDbSession.getDatabase("DBNAME");
Document newdoc = new Document();
Map<String , String> properties = new HashMap<String,String>();
properties.put(STUDENT_KEY_NAME, "REDDY");
properties.put(STUDENT_KEY_MARKS, "90");
properties.put(STUDENT_KEY_ROLL, "007");
newdoc.putAll(properties);
studentCouchDb.saveDocument(newdoc); 

For more information you can also refer Adding Document Using Java Couchdb4j.



来源:https://stackoverflow.com/questions/36817110/how-to-connect-couchdb-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!