Jun 12 2012
Data insertion to CouchDB using Lightcouch lib in Java (Sample)
Introduction
Hello, a small sample on how to insert JSON data to couchDB.
First off there are MANY java libraries for accessing couchdb. For information on which one to choose for you the 2 links below may help:
1) http://stackoverflow.com/questions/1105807/which-java-library-libraries-for-couchdb
2) http://wiki.apache.org/couchdb/Getting_started_with_Java
In this post we made use of LightCouch as we wanted something relatively simple without object mappings and other such features.
Aside: an also interesting tutorial utilizing couchdb4j with description of sessions is: http://www.drdobbs.com/jvm/223100116
Getting Started
We followed the instructions at: http://www.lightcouch.org/getstarted.html related to lightcouch 0.04 .
However, we included the actual lightcouch sources within our project in order to be able to see the actual implementation underneath. We did this by downloading the souce code utilizing Git Bash for windows.
Small modification for .properties file
As I had access to the actual couchDB source code I changed the readConfigFile method in order to fallback loading with properties.load:
properties.load(new FileInputStream(file));
This is a personal decision, using the default is fine as well:
Thread.currentThread().getContextClassLoader().getResource(resource)
If you are getting the error below a quick hack would be to edit your readConfigFile method to look similar to the following code.
"Could not read configuration file from the classpath:couchdb.properties"
Modified Load Method:
private void readConfigFile(String configFile) { try { InputStream inStream = getURL(configFile).openStream(); properties.load(inStream); } catch (Exception e) { boolean alternative_not_worked = true; try { //try to load from file log.info("Attempting to read config file directly from directory instead of classpath."); properties.load(new FileInputStream(configFile)); alternative_not_worked = false; } catch (FileNotFoundException e1) { log.error("Attempted to read config file directly from directory instead of classpath: File not Found"); } catch (IOException e1) { log.error("Attempted to read config file directly from directory instead of classpath: IO Exception"); } if(alternative_not_worked) { String msg = "Could not read configuration file from the classpath or directory: " + configFile; log.error(msg); throw new IllegalStateException(msg, e); } }
Actual Java Code
You need to add the following .jar to your project as libraries:
The code below shows the basic functionality of adding JSON documents to couchDB. (Editing to follow – eclipse project can be downloaded from here).
import java.util.Collections; import java.util.HashMap; import java.util.Map; import org.lightcouch.CouchDbClient; import org.lightcouch.Response; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import com.google.gson.JsonSerializer; //Based on http://www.lightcouch.org/lightcouch-guide.html#dependencies public class TestConnection { /** * @param args */ public static void main(String[] args) { CouchDbClient dbClient = new CouchDbClient("couchdb.properties"); Response resp; //How to insert JSON already formatted JsonParser parser = new JsonParser(); String json_text = "{"_id": "a"+(int)(Math.random()*100)+"","a": "A"}"; System.out.println(json_text ); JsonObject o = (JsonObject)parser.parse(json_text ); try { resp = dbClient.save(o); } catch(org.lightcouch.DocumentConflictException e) { //if we insert something that already exists //we get Exception in thread "main" org.lightcouch.DocumentConflictException: << Status: 409 (Conflict) } //Inserting some Json documents for(int i=0; i<100; i++) { JsonObject object=new JsonObject (); object.addProperty("name","Amit Kumar"); object.addProperty("Max.Marks",new Integer(100)); object.addProperty("Min.Marks",new Double(40)); object.addProperty("Scored",new Double(66.67)); object.addProperty("nickname","Amit"); object.addProperty("_id", "whatever"+(int)(Math.random()*100000)); object.addProperty("rev", "3"); try { resp = dbClient.save(object); } catch(org.lightcouch.DocumentConflictException e) { //if we insert something that already exists //we get Exception in thread "main" org.lightcouch.DocumentConflictException: << Status: 409 (Conflict) } } dbClient.shutdown(); }
Jul 22, 2012 @ 04:58:48
Hi Menelaos,
Thank you for good article.
I have used Lightcouch in my project and I ‘m pretty happy with it.
Experiencing with Views and design docs in lightcouch has made it alot easier to implement.
Copying the library sources into your project might not be a best practice generally. Though it might make sense as the code base is nothing big.
Jul 22, 2012 @ 15:13:52
Hey, glad you liked it!
I’m also happy with light couch. We’re using it in a project with a j2ee servlet for a gateway between couchdb and users.
One issue we’re wondering about is:
can you insert JSON text directly without having to make a JSON object ?
Obviously the JSON object is converted to a string at some point to send the HTTP call to the REST interface…
Your right about copying the code…I have a habit of wanting to poke around and see what’s going on with the original code though of course altering it will cause incompatibilities with future updates. Except ofcourse if it’s something useful and they include it after contribution.
Thanks – If you have any experience with the direct JSON insert drop a line 🙂
Jul 22, 2012 @ 17:06:37
Hi,
I think using JsonParser works well for converting a JSON String to a JSON object ready to persist.
Jul 24, 2012 @ 03:06:06
Indeed…though it adds a step if you already have the text ready. 🙂
http://mbakopoulos.wordpress.com/2012/07/24/add-new-json-document-using-plain-text-string-with-lightcouch/
Jun 26, 2013 @ 13:54:36
I have included the files to the project but the project throws source not found error when i try to create CouchDbClient object