Skip to main content

Easy-Cassandra version 2.0.1

Posted by otaviojava on February 13, 2014 at 5:45 PM PST

The new version of Easy-Cassandra, the version 2.0.1, have launched, some news are:

  • Refactoring constructor on Session Factory
  • QueryBuilder (InsertBuilder, UpdateBuilder, DeleteBuilder, SelectBuilder and BatchBuilder)
  • Go faster both write and read
  • Processors asynchronous
  • More supports to Spring-Data

As have an easier the configuration a Session Factory, it was made the ClustInformation, this way do more configuration with less code.


ClusterInformation clusterInformation = ClusterInformation.create()
                .addHost(HOST)
                .withDefaultKeySpace(KEY_SPACE).withUser(USER).withPassword(PASS);
        easyCassandraManager = new EasyCassandraManager(clusterInformation);


This way the Spring Cassandra configuration has changed.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
<xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/beans
          <context:component-scan" title="http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<context:component-scan">http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
&...
base-package="org.easycassandra.persistence.cassandra.spring" />

<bean id = "clusterInformation" class="org.easycassandra.persistence.cassandra.ClusterInformation">
<property name="keySpace" value="campus" />
<property name="hosts">
<list>
<value>localhost</value>
</list>
</property>
</bean>

<bean id="cassandraFactory" class="org.easycassandra.persistence.cassandra.spring.CassandraFactoryAnnotation">
<constructor-arg name="clusterInformation" ref="clusterInformation" />
<property name="annotatedClasses">
<list>
<value>org.easycassandra.persistence.cassandra.spring.entity.Contact</value>
            <value>org.easycassandra.bean.model.Step</value>
            <value>org.easycassandra.bean.model.Weight</value>
</list>
</property>
</bean>

<bean id="cassandraTemplate" class="org.easycassandra.persistence.cassandra.spring.SimpleCassandraTemplateImpl" >
<constructor-arg name="factory" ref="cassandraFactory" />
</bean>

</beans>

Configuration to Spring data on Easy-Cassandra

Maybe the most important improvement on this version is the QueryBuilder, the querybuilders are easy mode to do interaction with Cassandra, do more with less code, also was included BatchBuilder.


 SimpleID id = new SimpleID();
    id.setIndex(ONE_HUNDRED_THIRTY);
    id.setKey(ONE_HUNDRED_THIRTY);
    UpdateBuilder update = template.updateBuilder(SimpleQueryBuilder.class, key);
    update.put("map", "name", "otavioMap").value("value", TEN_DOUBLE).execute();
Example of update

UpdateBuilder update = persistence.updateBuilder(SimpleQueryBuilder.class);
update.whereEq(Constant.KEY, Constant.ONE).whereEq(Constant.INDEX, Constant.ONE).addList("list", "otavioList").execute();
 UpdateBuilder update = persistence.updateBuilder(SimpleQueryBuilder.class);
update.whereEq(Constant.KEY, Constant.ONE).whereEq(Constant.INDEX, Constant.ONE).value("value", 12D).executeAsync(new ResultAsyncCallBack() {
    @Override
    public void result(Boolean bean) {
        // do some action
    }
});
run an update asynchronously



InsertBuilder insert= persistence.updateBuilder(SimpleQueryBuilder.class);
Set set = new HashSet<>();
set.add("Linda");
Map map = new HashMap<>();
map.put("love", "Otavio and Poliana");
insert.value(Constant.KEY, Constant.ONE_HUNDRED).value(Constant.INDEX,
            Constant.ONE_HUNDRED).value(Constant.LIST_COLUMN, Arrays.asList("Poliana", "Otavio", "Love"))
.value(Constant.SET_COLUMN, set).value("map", map).execute();

Do insert

  SimpleID id = new SimpleID();
    id.setIndex(ONE_HUNDRED_TWO);
    id.setKey(ONE_HUNDRED_TWO);
    SimpleBean simpleBean = new SimpleBean();
    simpleBean.setId(id);
    simpleBean.setValue(VALUE);
    InsertBuilder insert2 = persistence.updateBuilder(simpleBean);
    insert2.executeAsync((new ResultAsyncCallBack() {
    @Override
    public void result(Boolean bean) {
        // do some action
    }
    });
Do an insert asynchronously

  DeleteBuilder delete = persistence.deleteBuilder(SimpleQueryBuilder.class); 
    delete.whereEq(Constant.INDEX, ONE_HUNDRED_TWO).whereEq(Constant.KEY, ONE_HUNDRED_TWO).execute();

    DeleteBuilder delete2 = persistence.deleteBuilder(SimpleQueryBuilder.class,"map", "list", "set");
    delete2 .whereEq(Constant.INDEX, ONE_HUNDRED_TWO).whereEq(Constant.KEY, ONE_HUNDRED_TWO).execute();

The first query remove all columns from key and the second one will remove just the map, list and set columns.

 SimpleID id = new SimpleID();
    id.setIndex(ONE_HUNDRED_TWO);
    id.setKey(ONE_HUNDRED_TWO);

    DeleteBuilder delete2 = persistence.deleteBuilder(SimpleQueryBuilder.class, id);
    delete2.executeAsync((new ResultAsyncCallBack() {
    @Override
    public void result(Boolean bean) {
        // do some action
    }
    });

Removing asynchronously.

  SelectBuilder select = persistence.selectBuilder(SimpleQueryBuilder.class);
  select.eq("name", "name");
  select.in("index", ONE, TWO, THREE);
  List result = select.execute();


SelectBuilder select = persistence.selectBuilder(SimpleQueryBuilder.class);
select.eq(NAME, NAME).gt("index", THREE).asc(INDEX).executeAsync(new ResultAsyncCallBack>() {

        @Override
        public void result(List beans) {
          // do some thing
        }
    });

Run asynchronously the query.


The batch is a feature on Cassandra, that allow you execute modification on database (insert, update and remove) as one, this way you can send inserts, updates and deletes as on request.



DeleteBuilder delete = dao.deleteBuilder();
    delete.whereEq(Constant.INDEX, Constant.ONE_HUNDRED_TWO)
            .whereEq(Constant.KEY, Constant.ONE_HUNDRED_TWO);

    InsertBuilder insert = dao.insertBuilder();
    insert.value(Constant.KEY, Constant.ONE_HUNDRED).value(Constant.INDEX,
            Constant.ONE_HUNDRED);
    insert.value(Constant.LIST_COLUMN,
            Arrays.asList("Poliana", "Otavio", "Love"));

    UpdateBuilder update = dao.update();
    update.whereEq(Constant.KEY, Constant.ONE).whereEq(Constant.INDEX, Constant.ONE);
    update.addList("list", "otavioList");

    BatchBuilder batchBuilder = dao.batchBuilder();

    batchBuilder.addOperations(delete, insert, update);
    batchBuilder.executeAsync(new ResultAsyncCallBack() {
        @Override
        public void result(Boolean bean) {
            // do some action
        }
    });
Run the inserts, deletes, updates atomically with BatchBuilder.

This article talked about the new version of Easy-Cassandra and show new its features.

  1. https://github.com/otaviojava/Easy-Cassandra/wiki/Builders
  2. https://github.com/otaviojava/Easy-Cassandra/wiki
  3. https://github.com/otaviojava/Easy-Cassandra/
  4. https://github.com/otaviojava/Easy-Cassandra-samples