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.


Sign in to add a comment
Powered by Google Project Hosting