My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Example  
Example code
Featured, Phase-Implementation
Updated Feb 4, 2010 by mbre...@gmail.com

Example

Session s = new Session("localhost",8888);
Database db = s.getDatabase("foodb");

Document doc = db.getDocument("documentid1234");
doc.put("foo","bar");
db.save(doc);

Document newdoc = new Document();
doc.put("foo","baz");
db.save(doc); // auto-generated id given by the database

// Running a view

ViewResult result = db.getAllDocuments(); // same as db.view("_all_dbs");
for (Document d: result.getResults()) {
	System.out.println(d.getId());

	/*
		ViewResults don't actually contain the full document, only what the view
		returned.  So, in order to get the full document, you need to request a
		new copy from the database.
	*/	Document full = db.getDocument(d.getId());
}

// Ad-Hoc view

ViewResult resultAdHoc = db.adhoc("function (doc) { if (doc.foo=='bar') { return doc; }}");

Comment by watso...@gmail.com, Dec 3, 2007

This is a very clean and useful interface. I am excited to take it for a test drive.

Comment by watso...@gmail.com, Dec 3, 2007

Grabbing the following jars from SVN is probably the easiest way to satisfy all of the dependencies.

  • lib/test/junit-4.1.jar
  • lib/test/easymock.jar
  • lib/test/log4j.jar
  • lib/commons-codec-1.3.jar
  • lib/commons-httpclient-3.1.jar
  • lib/json-lib-2.0-jdk15.jar
  • lib/commons-collections.jar
  • lib/commons-lang.jar
  • lib/commons-logging-1.1.jar
  • lib/commons-beanutils.jar
  • lib/ezmorph-1.0.3.jar
Comment by steve.ma...@gmail.com, Sep 8, 2008

Should "ViewResult?" be "ViewResults?" and db.save() be db.saveDocument() ??

Running this demo against the latest SVN needs those changes to work.

Comment by albert.m...@gmail.com, Dec 30, 2008

If you use Maven, here's the pom file that will get you going. Save it along with the jar as as %REPO%/com/fourspaces/couchdb4j/couchdb4j/0.1.2/couchdb4j-0.1.2.pom

<?xml version="1.0"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion> <groupId>com.fourspaces</groupId> <artifactId>couchdb4j</artifactId> <version>0.1.2</version> <packaging>jar</packaging> <name>CouchDB4j</name>
<url/>
<description>
Java API for accessing the REST interface of CouchDB (or FeatherDB)
</description>
<dependencies>
<dependency>
<groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version>
</dependency> <dependency>
<groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version>
</dependency> <dependency>
<groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.3</version>
</dependency>
<dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version>
</dependency> <dependency>
<groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.4</version>
</dependency> <dependency>
<groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1</version>
</dependency> <dependency>
<groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.2</version> <classifier>jdk15</classifier>
</dependency>
</dependencies>

</project>

Comment by albert.m...@gmail.com, Dec 30, 2008

BTW, Just tried it with featherDB instead of couchDB. Apparently FeatherDB do not have update_seq as a DB attribute, and thus session.getDatabase() will throw an exception. You can hack it by modifying the couchdb4j Database.java class, in the constructor:

if (json.containsKey("update_seq")) {
updateSeq = json.getInt("update_seq");
} else {
updateSeq = 0;
}

now, both couchDB4j and FeatherDB would work nicely :)

Comment by oyvind.l...@gmail.com, Mar 7, 2009

Theres a subtle error in the example:

Document newdoc = new Document(); doc.put("foo","baz"); db.save(doc); // auto-generated id given by the database

newdoc is created and never used.The already present doc variable is used altered and saved.

Comment by jmsa...@gmail.com, Jul 9, 2009

@oyvind: agreed.

It would also be really nice to flesh out this example a bit more and add:

1) non-simple values (e.g. a real JSON object, not just a string) 2) attachments

as neither are obvious.

Comment by jithinra...@gmail.com, Nov 19, 2012

hi, I face a exception at Session dbSession = new Session("localhost", 5984); when i try to run the program

Exception in thread "main" java.lang.NoClassDefFoundError?: org/apache/commons/httpclient/HttpException?

at Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException?: org.apache.commons.httpclient.HttpException?
at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController?.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader?.loadClass(ClassLoader?.java:306) at sun.misc.Launcher$AppClassLoader?.loadClass(Launcher.java:301) at java.lang.ClassLoader?.loadClass(ClassLoader?.java:247) ... 1 more

i have used the following jars commons-codec-1.6.jar commons-logging-1.1.1.jar couchdb4j-0.1.2.jar fluent-hc-4.2.2.jar gson-2.2.2.jar httpclient-4.2.2.jar httpclient-cache-4.2.2.jar httpcore-4.2.2.jar httpmime-4.2.2.jar

Comment by leningovi@gmail.com, Feb 18, 2013

How to get attachment from document using db.getAttachement. the attachment asking two parameters such as id and attachment body, i could not understand what is attachment body and what is attachment body value


Sign in to add a comment
Powered by Google Project Hosting