Get Started with LightCouch
LightCouch is a Java API for CouchDB database.
It provides a client interface to communicate with the database server HTTP API.
- JSON/Java mapping is handled by Gson library in LightCouch.
- HTTP communications by HttpClient
Usage Example
Maven
<dependency> <groupId>org.lightcouch</groupId> <artifactId>lightcouch</artifactId> <version>0.2.0</version> </dependency>
Manual download.
Configuration parameters typically declared in a file couchdb.properties and saved on the application default classpath, containing the following properties:
couchdb.name=testdb couchdb.createdb.if-not-exist=true couchdb.protocol=http couchdb.host=127.0.0.1 couchdb.port=5984 couchdb.username= couchdb.password=
See available options for advanced use.
A client i.e. CouchDbClient
is the main object in LightCouch used to access the database.
Initializing a new instance using the default constructor reads properties from couchdb.properties,
alternatively use an overloaded constructor.
CouchDbClient dbClient = new CouchDbClient(); Foo foo = new Foo(); // Plain Java Object Response response = dbClient.save(foo); Foo foo = dbClient.find(Foo.class, "doc-id"); dbClient.update(foo); dbClient.remove(foo); boolean b = dbClient.contains("doc-id"); // views List<Foo> list = dbClient.view("ddoc/by_date") .key("key") .includeDocs(true) .query(Foo.class); long count = dbClient.view("ddoc/by_tag") .key("couchdb").queryForLong(); // attachments Response response = dbClient.saveAttachment(inputStream, "photo.png", "image/png"); InputStream in = dbClient.find("doc-id/photo.png"); in.close(); // replication ReplicationResult replication = dbClient.replication() .source("source-db").target("target-db") .createTarget(true) .trigger(); // changes feed Changes changes = dbClient.changes() .includeDocs(true) .heartBeat(30000) .continuousChanges(); while (changes.hasNext()) { ChangesResult.Row feed = changes.next(); } // shutdown the client dbClient.shutdown();
Use in Spring application:
<bean id="dbClient" class="org.lightcouch.CouchDbClient" destroy-method="shutdown"/>
// Autowire in Spring Components @Autowired private CouchDbClient dbClient;
Refer to the Documentation for insight usage information.
Check example code in the test package.