Skip to content

Kundera with Couchdb

chhavigangwal edited this page Jun 5, 2015 · 11 revisions

Couchdb

Couchdb is an open source database that enables easy access of data over web. It uses JavaScript Object Notation (JSON) for data storage, a lightweight format based on a subset of JavaScript syntax. Couchdb can be easily downloaded and installed with help of instructions available on its site.

Support

Being a JPA provider, Kundera provides support for Couchdb. It allows to perform CRUD and query operation over Couchdb using JPA specifications. Let's look into how to use Kundera over Couchdb as database.

  • Entity
*/
@Entity
public class PersonCouchDB
{

    /** The person id. */
    @Id
    @Column(name = "PERSON_ID")
    private String personId;

    /** The person name. */
    @Column(name = "PERSON_NAME")
    private String personName;

    /** The age. */
    @Column(name = "AGE")
    private Integer age;
  // setters and getters. 
}


Persistence unit configuration

 <persistence-unit name="couchdb_pu">
                <provider>com.impetus.kundera.KunderaPersistence</provider>
                <properties>
                        <property name="kundera.nodes" value="localhost" />
                        <property name="kundera.port" value="5984" />
                        <property name="kundera.keyspace" value="couchdatabase" />
                        <property name="kundera.dialect" value="couchdb" />
                        <property name="kundera.client" value="couchdb" />
                        <property name="kundera.ddl.auto.prepare" value="create" />
                        <property name="kundera.client.lookup.class"
                                value="com.impetus.client.couchdb.CouchDBClientFactory" />
                        <property name="kundera.cache.provider.class"
                                value="com.impetus.kundera.cache.ehcache.EhCacheProvider" />
                        <property name="kundera.cache.config.resource" value="/ehcache-test.xml" />
                </properties>
</persistence-unit>


CRUD

final String nodeId = "node1";
final String originalName = "vivek";
PersonCouchDB object = new PersonCouchDB();
object.setAge(32);
object.setPersonId(ROW_KEY);
object.setPersonName(originalName);
object.setDay(Day.TUESDAY);
object.setMonth(Month.JAN);

Node node = new Node(nodeId, PersonCouchDB.class, new TransientState(), null, ROW_KEY);
node.setData(object);
client.persist(node);

PersonCouchDB result = (PersonCouchDB) client.find(PersonCouchDB.class, ROW_KEY);

```
```
// Find by key and now row key
String findByIdAndAge = "Select p from PersonCouchDB p where p.personId=:personId AND p.age=:age";
query = em.createQuery(findByIdAndAge);
query.setParameter("personId", ROW_KEY);
query.setParameter("age", 32);
```

* **Range**
```
 // Find by greater than and less than clause over non row key
String findAgeByGTELTEClause = "Select p from PersonCouchDB p where p.age <=:max AND p.age>=:min";
query = em.createQuery(findAgeByGTELTEClause);
query.setParameter("min", 32);
query.setParameter("max", 35);
```

* **Between clause**
```
 // find by between over non rowkey
String findAgeByBetween = "Select p from PersonCouchDB p where p.age between :min AND :max";
query = em.createQuery(findAgeByBetween);
query.setParameter("min", 32);
query.setParameter("max", 35);
```
* **Select specific field (without where clause)**
```
 // select specific field AGE
String findAge = "Select p.age from PersonCouchDB p";
query = em.createQuery(findAge);
```
* **Perform aggregations (without where clause)**

Kundera supports COUNT MAX MIN SUM and AVG aggregation queries with CouchDB. Currently the support is enabled for queries without WHERE clause only.
```
 // run aggregation like MIN SUM COUNT etc
String findMinAge = "Select MIN(p.age) from PersonCouchDB p";
query = em.createQuery(findMinAge);
String findSumAge = "Select SUM(p.age) from PersonCouchDB p";
query = em.createQuery(findSumAge);
String findCountAge = "Select COUNT(p.age) from PersonCouchDB p";
query = em.createQuery(findCountAge);
```
* Please refer [CouchDBQueryTest](https://github.com/impetus-opensource/Kundera/tree/trunk/src/kundera-couchdb/src/test/java/com/impetus/client/couchdb/query) for more details over JPA query support.

**_You may also refer [junits](https://github.com/impetus-opensource/Kundera/tree/trunk/src/kundera-couchdb/src/test/java/com/impetus/client/couchdb) for more examples._**

### To-do
* Enable SELECT query on a particular column with WHERE clause
* Nested AND clause support
Clone this wiki locally