Skip to main content

Persisting information with Cassandra in java: Simple example

Posted by otaviojava on January 24, 2012 at 3:16 AM PST


This article has the main objective show a little example for persist information in Cassandra using java.

For demonstrated the persistence with Cassandra will used the Easy-Cassandra, a framework open source for use this SGBG in an easy mode.
 
@ColumnFamilyValue(nome = "person")//
public class Person implements Serializable {
 
private static final long serialVersionUID = 3L;
@KeyValue(auto=false)
private Long id;
@IndexValue
@ColumnValue(nome = "name")
private String name;
@ColumnValue(nome = "year")
private Integer year;
@EnumeratedValue(nome="sex") campo
private Sex sex;
@EmbeddedValue
private Address address;
//getter and setter
}
 
 
 
public class PersonDAO {
 
private Persistence persistence;
 
public PersonDAO() {
persistence = EasyCassandraManager.getPersistence("javabahia", "localhost", 9160);
}
 
public void create(Person bean) {
persistence.insert(bean);
}
 
 
public void remove(Person bean) {
persistence.delete(bean);
}
 
public void remove(Long id){
persistence.deleteByKeyValue(id, Person.class);
}
public void update(Person bean) {
 
persistence.update(bean);
 
}
 
public Person retrieve(Object id) {
return (Person) persistence.findByKey(id, Person.class);
}
 
@SuppressWarnings("unchecked")
public List listAll() {
return persistence.findAll(Person.class);
}
 
 
@SuppressWarnings("unchecked")
public List listByIndex(Object index) {
return persistence.findByIndex(index, Person.class);
}
 
}
 
 
The source can be downloaded in a link, in the end of article, when you are seeing the source can see the annotations bellow:
 
 
  • ColumnFamilyValue: annotations for identify the family column name
  • ColumnValue: for identify the column in the Family Column, the classes can be used are:
  • java.lang.Boolean
  • java.util.Date
  • java.lang.Double
  • java.lang.Float
  • java.lang.Integer
  • java.lang.Long
  • java.lang.String
  • EmbeddedValue: The class with this is annotation has fields with ColumnValue inside itself, but the persistence way continues in the same Family of the Column. This annotation is to do the object’s modeling easily.
  • EnumeratedValue: for be used in Enums
  • IndexValue: The field with this annotation is an index, can search and retrieve information from the row like KeyValue, need also use the ColumnValue together with this annotation.
  • KeyValue: The field with this annotation is an Key of the Row. If the auto value is true will generate auto increment, each new Key in the FamilyColumn there are a new value auto numeric.
 
The annotation ColumnFamilyValue, ColumnValue and EnumeratedValue if the value “name” be empty the value default is the Field's name.
 
After see the code let go that for run, so you need:
 
 
  • JDK 7 or above
For run the example follow this the steps:
 
  • Do download of the apache-cassandra in: Here
  • After that, unzip the file downloaded and enter it.
  • Execute the command when you stay in Cassandra home:
./bin/cassandra -f
  • For execute the client's mode, also in Cassandra'home execute the command bellow:
./bin/cassandra-cli -host localhost
 
  • In the Client's mode run the script bellow:
 
create keyspace javabahia; use javabahia; create column family person with comparator = UTF8Type and column_metadata = [   {column_name: name, validation_class: UTF8Type, index_type: KEYS} ];

 

  • Download and add in ClassPath of the Project, the Easy-Cassandra's lib:
  • You will have two choices of Download:
  • Only the Easy-Cassandra's lib
  • The Easy-Cassandra's lib with its dependencies (The Libs of the Thrift)
This article has like main objective show a little some persist of information in apache-Cassandra, for this was used a little example that is available in Netbeans and Eclipse IDE.

Reference:
 
Download of sources and libs
 
 
Wiki Easy-Cassandra:
 
 
home Easy-Cassandra: