Skip to main content

Persist document in Cassandra

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


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.
public class Photo {
private String name;
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) {
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 and all classes who implement java.nio.file.Path.


 Example program with Eclipse and Netbeans: