Skip to main content

Persist document in Cassandra

Posted by otaviojava on February 1, 2012 at 11:32 AM PST

font:http://www.gerenciandoblog.com.br/2009/08/onde-hospedar-arquivos-para-seu-blog.html

 
 
Nowadays the Enterprise applications beyond persist String and number also can save file. Persist this information is very interesting, for example, a civil process there are information about the process (name of author, date, number of protocol) and the document which represents, or a twett with an image. In Apache Cassandra, you can save file, but for large file you should use a NOSQL Document Store.
 
For demonstrated this resource will made a little program, an album of photography, The picture will show from name. If I use “Paris” will show a picture was related to that name.
 
 
 
 
The program was made with java SE 7 platform, with Swing like GUI, and Easy-Cassandra framework, for this it's necessary download of Easy-Cassandra and its dependencies.
 
The object has two field:
The name of the photo, how this field must be unique it also will the key
The file of the photo
 
The table 1 show the object made.
 
 
@ColumnFamilyValue
public class Photo {
@KeyValue
private String name;
 
@ColumnValue
private File picture;
//getter and setter
}
Table 1: The Object made
 
 
public class PhotoDao {
private Persistence persistence;
public PhotoDao() {
persistence = EasyCassandraManager.getPersistence("exemplo", "localhost", 9160);
}
public void criar(Photo bean) {
persistence.insert(bean);
}
 
@SuppressWarnings("unchecked")
public List listarTodos() {
return persistence.findAll(Photo.class,ConsistencyLevelCQL.ALL);
}
}
Table 2: The DAO
 
When the Cassandra is running the next step is create the KeyStore and Family Column, in Cassandra's Client mode execute the command in the table 3.
 
 

create keyspace exemplo; use exemplo;

create column family Photo

with comparator = UTF8Type;

Table 3; Command for run

 
 
 
 
 
This post presented the persistence of an document or file with a simple example. This resource is useful and easy of use. The Easy-Cassandra has support with java.io.File and all classes who implement java.nio.file.Path.
 
 

Reference:

Easy-Cassandra: https://github.com/otaviojava/Easy-Cassandra/
 Example program with Eclipse and Netbeans: https://github.com/otaviojava/Easy-Cassandra/downloads