otaviojava's Blog
Easy-Cassandra 1.0.9 Final : With annotations inheritance, jpa and JPQL
Release the newest version of the framework to persist objects in Apache Cassandra in easy way. Among improvement is the JPA annotations, also JPQL.
JCassandra jCassandra=persistence.createJCassandra("select * from Person");
List<Person> persons=jCassandra.getResultList();table 1: sample using JPQL in Easy-Cassandra
This version has the improvements below:
-
Supporting at jpa 2.0 annotation
-
Query with objects
-
Version to retrieve the timestamp of the object
-
Using the JPQL
-
now supported inheritance
|
Easy-Cassandra 1.0.8
|
Easy-Cassandra 1.0.9 (Com JPA anotação)
|
|
org.easycassandra.annotations.ColumnFamilyValue
|
javax.persistence.Entity
|
|
org.easycassandra.annotations.ColumnValue
|
javax.persistence.Column
|
|
org.easycassandra.annotations.EmbeddedValue
|
javax.persistence.Embedded
|
|
org.easycassandra.annotations.EnumeratedValue
|
javax.persistence.Enumerated
|
|
org.easycassandra.annotations.KeyValue
|
javax.persistence.Id
|
|
org.easycassandra.annotations.KeyValue equals
true |
javax.persistence.GeneratedValue
|
|
org.easycassandra.annotations.IndexValue
|
org.easycassandra.annotations.Index
|
Also was added functionality for the @Version annotation, its objective is see the timestamp common between fields in objects.
@Entity(name = "autocount")
public class Count implements Serializable {
private static final long serialVersionUID = 3L;
@Id
@GeneratedValue
private BigInteger id;
@Column(name = "name")
private String name;
@Version
private Long timeStampLong;
@Version
private Date timeStampDate;
@Version
private Calendar timeStampCalendar;
//getter and setter
}table 3: using the Version annotation
Also you can use inheritance for this you need the @MappedSuperclass annotations in father's class and the subclass allow persist the objects of the super class.
@MappedSuperclass
public class Worker implements Serializable {
private static final long serialVersionUID = -5568409094833637814L;
@Column
private String name;
@Column
private Double salary;
//getter and setter
@Entity
public class Engineer extends Worker {
@Id
private String nickName;
@Column
private String type;
@Column
private String especialization;
//getter and setter
}
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 206 reads
Easy-Cassandra 1.0.9: With annotations jpa and JPQL
Release the newest version of the framework to persist objects in Apache Cassandra in easy way. Among improvement is the JPA annotations, also JPQL.
JCassandra jCassandra=persistence.createJCassandra("select * from Person");
List<Person> persons=jCassandra.getResultList();table 1: sample using JPQL in Easy-Cassandra
|
Easy-Cassandra 1.0.8
|
Easy-Cassandra 1.0.9 (Com JPA anotação)
|
|
org.easycassandra.annotations.ColumnFamilyValue
|
javax.persistence.Entity
|
|
org.easycassandra.annotations.ColumnValue
|
javax.persistence.Column
|
|
org.easycassandra.annotations.EmbeddedValue
|
javax.persistence.Embedded
|
|
org.easycassandra.annotations.EnumeratedValue
|
javax.persistence.Enumerated
|
|
org.easycassandra.annotations.KeyValue
|
javax.persistence.Id
|
|
org.easycassandra.annotations.KeyValue equals
true |
javax.persistence.GeneratedValue
|
|
org.easycassandra.annotations.IndexValue
|
org.easycassandra.annotations.Index
|
Also was added functionality for the @Version annotation, its objective is see the timestamp common between fields in objects.
@Entity(name = "autocount")
public class Count implements Serializable {
private static final long serialVersionUID = 3L;
@Id
@GeneratedValue
private BigInteger id;
@Column(name = "name")
private String name;
@Version
private Long timeStampLong;
@Version
private Date timeStampDate;
@Version
private Calendar timeStampCalendar;
//getter and setter
}table 3: using the Version annotation
For show the new resources for framework will do an example with musical albuns that can call for any platform, this way, will use web services. For do this sample will use the Java EE 6 platform. After object modeling like show table 4.
@XmlRootElement
@Entity
public class Album implements Serializable{
@Id
private Long id;
@Column(name="nameOfAlbum")
private String name;
@Column
private String style;
@Column
private Calendar release;
@Column
private Double price;
@Embedded
private Artist artist;
//getter and setter
}
@XmlRootElement
public class Artist implements Serializable{
/**
* name of the artist
*/
@Column(name="nameOfArtist")
private String name;
/**
* was born
*/
@Column
private String country;
@Enumerated
private Sex sex;
//getter and setter
}
@XmlRootElement
public enum Sex {
MALE,FAMALE
}table 4: class with jpa annotations for Easy-Cassandra
With the class done, now will do the JPQL the Jcassandra's interface extended javax.persistence.Query, so you may use the Query interface if you wat. But there are some method not implement yet.
Other thing important is the way how the Easy-Cassandra manager the classes. It no use xml. To manage use the method addFamilyObject in the EasyCassandraManager Class. You should add the Class before use in the jpql.
@ApplicationScoped
public class PersistenceManagement {
@Produces
private Persistence persistence;
@Inject
public void init(){
persistence=EasyCassandraManager.getPersistence("javabahia", "localhost", 9160);
EasyCassandraManager.addFamilyObject(Album.class);
}
}
@RequestScoped
public class PersistenceService {
@Inject
private Persistence persistence;
public List<Album> findAllAlbum() {
Query query=persistence.createJCassandra(" select * from Album");
return query.getResultList();
}
public Album findById(Long id) {
Query query=persistence.createJCassandra(" select * from Album where id =:id");
query.setParameter("id", id);
return (Album) query.getSingleResult();
}
public boolean save(Album album) {
return persistence.insert(album);
}
public void remove(Long id) {
persistence.deleteByKeyValue(id, Album.class);
}
public String update(Long id, String name, String style, Double price,String nameOfArtis,String country) {
StringBuilder query=new StringBuilder();
AuxQuery auxQuery=new AuxQuery();
String conecion="";
query.append("update Album set ");
if(!isnull(name)){
query.append(conecion);
query.append(" name =:name ");
auxQuery.updatable=true;
conecion=" , ";
}
if(!isnull(style)){
query.append(conecion);
query.append(" style =:style ");
auxQuery.updatable=true;
conecion=" , ";
}
if(!isnull(price)){
query.append(conecion);
query.append(" price =:price ");
auxQuery.updatable=true;
}
if(!isnull(country)){
query.append(conecion);
query.append(" artist.country =:country ");
auxQuery.updatable=true;
conecion=" , ";
}
if(!isnull(nameOfArtis)){
query.append(conecion);
query.append(" artist.name =:artist ");
auxQuery.updatable=true;
}
query.append(" where id =:id ");
if(!auxQuery.updatable){
return "nothing is update";
}
Query jCassandra=persistence.createJCassandra(query.toString());
if(!isnull(name)){
jCassandra.setParameter("name", name);
}
if(!isnull(style)){
jCassandra.setParameter("style", style);
}
if(!isnull(price)){
jCassandra.setParameter("price", price);
}
if(!isnull(nameOfArtis)){
jCassandra.setParameter("artist", nameOfArtis);
}
if(!isnull(country)){
jCassandra.setParameter("country", country);
}
jCassandra.setParameter("id", id);
return jCassandra.executeUpdate()==1?"executed with sucess":" error in update ";
}
class AuxQuery{
boolean updatable=false;
}
private boolean isnull(Object object){
return object==null;
}
}table 5: do jpql in Cassandra
Then, now will create the resource for restful's service like show the table 6.
@Path("/album")
@RequestScoped
public class AlbumResource {
@Inject
private PersistenceService persistenceService;
@GET
@Produces(MediaType.TEXT_XML)
public List<Album> getProdutos() {
return persistenceService.findAllAlbum();
}
@Path("{id}")
@GET
@javax.ws.rs.Produces(MediaType.TEXT_XML)
public Album getAlbum(@PathParam("id") long id) {
return persistenceService.findById(id);
}
@Path("update/{id}")
@GET
@javax.ws.rs.Produces(MediaType.TEXT_PLAIN)
public String updateAlbum(@PathParam("id") Long id ,@QueryParam("name") String name,@QueryParam("style") String style,@QueryParam("price") Double price,@QueryParam("artist") String nameOfArtis,@QueryParam("country") String country) {
return persistenceService.update(id,name,style,price,nameOfArtis,country);
}
@POST
@Consumes(MediaType.TEXT_XML)
@Produces(MediaType.TEXT_PLAIN)
public String addAlbum(Album album) {
boolean sucess=persistenceService.save(album);
if(sucess){
return album.getName() + " added!";
}
return album.getName() + " not added!";
}
@Path("{id}")
@DELETE
@Produces(MediaType.TEXT_PLAIN)
public String removeAlbum(@PathParam("id") Long id) {
persistenceService.remove(id);
return "Album removed.";
}
}
table 6: create rest service with JAX-RS
One way for test is using the poster, a plugin for firefox. With this plugin you able do test in all resources (post, get and remove), but just with browser you able test get methods.
|
list of album
|
|
|
Object album with id equals 1
|
|
|
Object album with id equals N
|
|
|
Insert or update an album wit id equals 12, with name equals Brasil, style eletronic, price 23 and artist me
|
table 7: sample with rest services.
With this has showed the improvements in new Easy-Cassandra version, the 1.0.9. Was showed the change with annotations, replacement now to jpa, and the jpql improvement that now supported in Easy-Cassandra.
Reference:
Sample rest service rest (AlbumServerRest.rar): https://github.com/otaviojava/Easy-Cassandra/downloads
Poster Plugin Firefox: https://addons.mozilla.org/en-US/firefox/addon/poster/
Easy-Cassandra: https://github.com/otaviojava/Easy-Cassandra/
Download for source and documentation to version 1.0.9 ( documento 03): https://github.com/otaviojava/Easy-Cassandra/downloads
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 1416 reads
Easy-Cassandra= java ORM for Cassandra, now run with Maven

Running the new version of the Easy-Cassandra framework it main objective is making it easy the communication between Apache Cassandra and your application in java. With it you able create, update, retrieve, delete the java's objects in easy way and simple, for this you must add some annotation in Class and fields.
Characteristics
-
An ORM easy to use in Cassandra
-
Need only use some Annotations in a class to persist
-
Persists many Java Objects in way extremely easy (e.g: all primitives types, java.Lang.String, java.lang.BigDecimal, java.io.File, see here for more details).
-
Read and Write in Cassandra with Level Consistency.
-
The first framework ORM in Cassandra to compatives with version above 0.8.0.
-
The first to use CQL
-
The first to use invokedynamic instead to reflection
-
In the apache version 2.0 license
improvement in 1.0.8
- Make of the Column Family and secondary index in real time
- CQL 2.0 Support
- Support call key with 'in' command
For show the new version, we will do a simple application. This application has the objective persist information about average income of Brazil group by CEP. The objective is show how much is generated in cash and number of commercial entities by street. This way will be persist around 640 thousands information for each survey. Each request has the information below:
-
Lote code
-
how may commercial entities in CEP
-
value in cash
-
cite
-
street
-
district
-
Cep
-
type of street
-
Province
-
acronym of Province
For amazing the data will use the Cassandra, its features are:
Cassandra
Proven
Cassandra is in for many companies. that have large, active data sets. The largest known Cassandra cluster has over 300 TB of data in over 400 machines.
Fault Tolerant
Data is automatically replicated to multiple nodes for fault-tolerance. Replication across multiple data centers is supported. Failed nodes can be replaced with no downtime.
Decentralized
There are no single points of failure. There are no network bottlenecks. Every node in the cluster is identical.
You're in Control
Choose between synchronous or asynchronous replication for each update. Highly available asynchronous operations are optimized with features like Hinted Handoff and Read Repair.
Rich Data Model
Allows efficient use for many applications beyond simple key/value.
Elastic
Read and write throughput both increase linearly as new machines are added, with no downtime or interruption to applications.
Durable
Cassandra is suitable for applications that can't afford to lose data, even when an entire data center goes down.
Professionally Supported
Cassandra support contracts and services are available from third parties.
font:http://cassandra.apache.org/
For get developing more easy will used the Easy-Cassandra, It works like a layer above of the Thrift, facilitating call for Cassandra, increasing more productivity and leaving code more clean. For use it is necessary download the framework and its dependencies. You can manage this libs using the maven, for this add the repository like table 1.

table1: add repository and call the Easy-Cassandra
Each request will a line in Cassandra which might vary in size line by line. This way the class with annotations was like show table 2.
|
@ColumnFamilyValue(name = "controle")
public class ControleRenda implements Serializable {
@KeyValue(auto = true)
private Long id;
@EmbeddedValue
private Endereco endereco;
@ColumnValue(name = "valor")
private Double valor;
@ColumnValue(name = "quantidade")
private Long quantidade;
//getter and setter
}
public class Endereco implements Serializable {
@ColumnValue(name="cidade")
private String cidade;
@ColumnValue(name="logradouro")
private String logradouro;
@ColumnValue(name="bairro")
private String bairro;
@IndexValue
@ColumnValue(name="cep")
private String cep;
@ColumnValue(name="tp_logradouro")
private String tp_logradouro;
@ColumnValue(name="estado")
private String estado;
@ColumnValue(name="sigla")
private String sigla;
// getter and setter
}
|
Table 2: Mapping class with Easy-Cassandra
When you see the table 2, will seen the Address class with an annotation @IndexValue in cep's field, this annotation does the field be a secondary index( The secondary index is other field for search information beyond the key row).
For run our sample application needs Cassandra is running and make the keystore. The column family and secondary index will be created in run time thanks improvement in this version.
[prettifyclass="western"]<span style="font-size: x-small; ">create keyspace renda;</span>[/prettify]
|
This way now only run the application and all information will persist in Cassandra. In this article was showed a practice example with 640 thousand requests. Was showed some improvements like create column family and secondary index in run time in this new version of the Easy-Cassandra.
References:
Easy-Cassandra: https://github.com/otaviojava/Easy-Cassandra
Documentation, lib e source (Maven_Example.rar): https://github.com/otaviojava/Easy-Cassandra/downloads
wiki: https://github.com/otaviojava/Easy-Cassandra/wiki
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 1586 reads
Knowing more about Easy-Cassandra Project
Features
- An ORM easy to use in Cassandra
- Need only use some Annotations in a class to persist
- Persists many Java Objects in way extremely easy (e.g: all primitives types, java.Lang.String, java.lang.BigDecimal, java.io.File, etc.).
- Read and Write in Cassandra with Level Consistency.
- The first framework ORM in Cassandra to compatives with version above 0.8.0.
- The first to use CQL
- compatible with CQL 2.0
- The first to use invokedynamic instead to reflection
- In the Apache version 2.0 license
Java Objects Supported
- all primitives types (int, long, float, double, short, byte, boolean)
- java.lang.Boolean
- java.util.Date
- java.lang.Double
- java.lang.Float
- java.lang.Integer
- java.lang.Long
- java.lang.String
- java.lang.Boolean
- java.lang.Byte
- java.lang.Short
- java.lang.Character
- java.io.File
- java.nio.file.Path
About Versions
The current and stable version is 1.0.7
Version: 1.0.7
- update cassandra-thrift to 1.0.7
Version: 1.0.6
- Fixes bug with File
- Support Calendar interface
Version: 1.0.5
- Can now store files
- Support java.io.File and java.nio.file.Path
Version: 1.0.4
- more performance
- less memory
- now is supported all primitives types
- now is supported Byte, character, Short, BigInteger and BigDecimal
Version: 1.0.3
- Fixes bug with result
- update for Thrift 1.0.6
- Log now using java.util.loggin
Version: 1.0.2
- Fixes bug with Boolean's Object
- Now the Cassandra's lib is supported this way is possible use every Cassandra above of the version 0.8.0
Version: 1.0.1
- Allowed use ColumnValue and ColumnFamilyValue in default mode this way its get the field's name
- Fixes bug in Reflection
Beyond version
- Call exception when there are not neither Index Key nor Key Value in the class
- Select Key from 'in' CQL command
- Create automatically the ColumnFamily in Run Time
- Create automatically the IndexValue in Run Time
- Suport with cql 2.0
- Support with one and more Index
- Feed Object with Cassandra Query Language
Example: List<Person> persons= cassandraQuery.executeQuery("select * from Person").getResultList();
Simple Sample
For use this recourse is very simple like sample bellow:
@ColumnFamilyValue(nome = "person")
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <b>class</b> Person <b>implements</b> <font color="#003399">Serializable <font color="#009900">{</font> </font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>private</b> <b>static</b> <b>final</b> <font color="#000066"><b>long</b> serialVersionUID <font color="#339933">=</font> 3L<font color="#339933">;</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@KeyValue<font color="#009900">(auto<font color="#339933">=</font><font color="#000066"><b>false</b></font><font color="#009900">)</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>private</b> <font color="#003399">Long id<font color="#339933">;</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@IndexValue</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@ColumnValue<font color="#009900">(nome <font color="#339933">=</font> <font color="#0000ff">"name"</font><font color="#009900">)</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>private</b> <font color="#003399">String name<font color="#339933">;</font> </font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@ColumnValue<font color="#009900">(nome <font color="#339933">=</font> <font color="#0000ff">"year"</font><font color="#009900">)</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>private</b> <font color="#003399">Integer year<font color="#339933">;</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@EnumeratedValue<font color="#009900">(nome<font color="#339933">=</font><font color="#0000ff">"sex"</font><font color="#009900">)</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>private</b> Sex sex<font color="#339933">;</font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@EmbeddedValue</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>private</b> Address address<font color="#339933">;</font></font></font>
<font color="#666666"><font size="2" style="font-size: 9pt"><i>//getter and setter</i></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <b>class</b> PersonDAO <font color="#009900">{</font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>private</b> Persistence persistence<font color="#339933">;</font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> PersonDAO<font color="#009900">() <font color="#009900">{</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">persistence <font color="#339933">= EasyCassandraManager.<font color="#006633">getPersistence</font><font color="#009900">(</font><font color="#0000ff">"javabahia"</font>, <font color="#0000ff">"localhost"</font>, <font color="#cc66cc">9160</font><font color="#009900">)</font><font color="#339933">;</font></font></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <font color="#000066"><b>void</b> create<font color="#009900">(</font>Person bean<font color="#009900">)</font> <font color="#009900">{</font></font></font></font>
<font color="#000000"> <font size="2" style="font-size: 9pt">persistence.</font><font color="#006633"><font size="2" style="font-size: 9pt">insert</font></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">bean</font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font color="#339933"><font size="2" style="font-size: 9pt">;</font></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <font color="#000066"><b>void</b> remove<font color="#009900">(</font>Person bean<font color="#009900">)</font> <font color="#009900">{</font></font></font></font>
<font color="#000000"> <font size="2" style="font-size: 9pt">persistence.</font><font color="#006633"><font size="2" style="font-size: 9pt">delete</font></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">bean</font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font color="#339933"><font size="2" style="font-size: 9pt">;</font></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <font color="#000066"><b>void</b> remove<font color="#009900">(</font><font color="#003399">Long</font> id<font color="#009900">){</font></font></font></font>
<font color="#000000"> <font size="2" style="font-size: 9pt">persistence.</font><font color="#006633"><font size="2" style="font-size: 9pt">deleteByKeyValue</font></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">id, Person.</font><font size="2" style="font-size: 9pt"><b>class</b></font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font color="#339933"><font size="2" style="font-size: 9pt">;</font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><font color="#009900">} </font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <font color="#000066"><b>void</b> update<font color="#009900">(</font>Person bean<font color="#009900">)</font> <font color="#009900">{</font></font></font></font>
<font color="#000000"> <font size="2" style="font-size: 9pt">persistence.</font><font color="#006633"><font size="2" style="font-size: 9pt">update</font></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">bean</font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font color="#339933"><font size="2" style="font-size: 9pt">;</font></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> Person retrieve<font color="#009900">(<font color="#003399">Object</font> id<font color="#009900">)</font> <font color="#009900">{</font></font></font></font>
<font color="#000000"> <font size="2" style="font-size: 9pt"><b>return</b></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">Person</font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font size="2" style="font-size: 9pt"> persistence.</font><font color="#006633"><font size="2" style="font-size: 9pt">findByKey</font></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">id, Person.</font><font size="2" style="font-size: 9pt"><b>class</b></font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font color="#339933"><font size="2" style="font-size: 9pt">;</font></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@SuppressWarnings<font color="#009900">(<font color="#0000ff">"unchecked"</font><font color="#009900">)</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <font color="#003399">List listAll<font color="#009900">()</font> <font color="#009900">{</font></font></font></font>
<font color="#000000"> <font size="2" style="font-size: 9pt"><b>return</b></font><font size="2" style="font-size: 9pt"> persistence.</font><font color="#006633"><font size="2" style="font-size: 9pt">findAll</font></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">Person.</font><font size="2" style="font-size: 9pt"><b>class</b></font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font color="#339933"><font size="2" style="font-size: 9pt">;</font></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#000000"><font size="2" style="font-size: 9pt">@SuppressWarnings<font color="#009900">(<font color="#0000ff">"unchecked"</font><font color="#009900">)</font></font></font></font>
<font color="#000000"><font size="2" style="font-size: 9pt"><b>public</b> <font color="#003399">List listByIndex<font color="#009900">(</font><font color="#003399">Object</font> index<font color="#009900">)</font> <font color="#009900">{</font></font></font></font>
<font color="#000000"> <font size="2" style="font-size: 9pt"><b>return</b></font><font size="2" style="font-size: 9pt"> persistence.</font><font color="#006633"><font size="2" style="font-size: 9pt">findByIndex</font></font><font color="#009900"><font size="2" style="font-size: 9pt">(</font></font><font size="2" style="font-size: 9pt">index, Person.</font><font size="2" style="font-size: 9pt"><b>class</b></font><font color="#009900"><font size="2" style="font-size: 9pt">)</font></font><font color="#339933"><font size="2" style="font-size: 9pt">;</font></font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
<font color="#009900"><font size="2" style="font-size: 9pt">}</font></font>
Sample of DAO performing call for the Cassandra
More information
If you would like do question about, contribute, share experience, do anything for the project. Do part of Group in Google Groups: https://groups.google.com/group/easy-cassandra/
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 1838 reads
Persist document in Cassandra

|
@ColumnFamilyValue
public class Photo {
@KeyValue
private String name;
@ColumnValue
private File picture;
//getter and setter
}
|
|
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<Photo> listarTodos() {
return persistence.findAll(Photo.class,ConsistencyLevelCQL.ALL);
}
}
|
|
|
Table 3; Command for run

Reference:
Easy-Cassandra: https://github.com/otaviojava/Easy-Cassandra/
Example program with Eclipse and Netbeans: https://github.com/otaviojava/Easy-Cassandra/downloads
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 1667 reads
Persisting information with Cassandra in java: Simple example

This article has the main objective show a little example for persist information in Cassandra using java.
|
@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
return persistence.findAll(Person.class);
}
@SuppressWarnings("unchecked")
public List
return persistence.findByIndex(index, Person.class);
}
}
|
-
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.
-
JDK 7 or above
-
Apache Cassandra is running here
-
Create the Keystore and Family column here
-
Do annotations in the class Annotations
-
Realize call for save the object in Cassandra Here
-
Add Easy-Cassandra's Lib lhere
-
Add Thrift Dependence: Easy Cassandra with Thrift dependences
-
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)
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 2294 reads
Killing the myths about java
-
The first advantage is because it's open source, so we can read, learn from its source.
-
With the java 7, it's implementation reference in other words if you want make a application for run in all JVM, if you use the openjdk is most safest than other JDK.
-
The java's community is very strong, maybe strongest in the world. The project for example has been undergoing improvements and for add some resource it's necesary test.
-
The Oracle donated the code ot JRockit and in the Java 8, scheduled for late 2013, the jvm will be integrated with HostSpot, in openjdk there will the better of two JVM in only one place.
-
Many company are in the project. The JVM has know-how may companies in the world. Companies like IBM, Apple, SAP, Mac, Azul, Intel, RedHat etc are in the project.
-
If Oracle forget the Java and don't the JVM. O openjdk don't will die because there are many companies have been donated time and work in the project also the community.
-
Integration between Jrockit and HotSpot
-
update code and libraries for the java 7
-
fixes bugs in java 7
-
works have been started in java 8
-
Conclusion in integration beteween jRockit and HotSpot making the HotRockit
-
JSR 308: Annotations on Java Types
-
JSR 310: Date and Time API
-
JSR TBD: More Small Enhancements to the Java Programming Language
-
JSR 335: Lambda Expressions for the Java Programming Language
-
JSR TBD: Java Platform Module System
-
Come a new version for the Java, thing don't happened in six year.
-
Come new features without a big JDK.
-
periodic update in other word a constant update's cycle.
-
Large modifications like lambda and jigsaw will be more mature.
11 – With this release what happen with the 6 ?
-
java 7 release
-
The openjdk is reference implementation
-
The java fx get a open source project
-
Planes for the java 8 and beyond
-
Java Me rise up and come back to work
-
Soujava and LondoJug do part in JCP
-
More work in JVM
-
More integration between JUGs around orbit
-
More companies using and contribute the openjdk
-
The javafx run anywhere
-
More four updates in java 7
-
Last public version for java 6
-
Release Java ME 7
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 4821 reads
Comments
The references about Java performance are kinda out of ...
by fcinter - 2012-01-20 06:04
The references about Java performance are kinda out of date and inaccurate.
There are too much article around the internet ...
by otaviojava - 2012-01-20 06:39
Thanks for adding the point about the perfromance of java ...
by extremejava - 2012-01-17 09:09
Thanks for adding the point about the perfromance of java based applications.
Party 10 year with Eclipse, around the Brazil

- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 997 reads
Moving o java forward part 4- JSR 166y: concurrent Package
Was added to his package to facilitate applications which needs scalable processes.
A feature is atomic variable. Variable atomic means that it cannot be divided - it's like the S.O. with any resource (Driver CD, USB) cannot be divided but needs be used in many processes.
|
Picture 1: Simple example with consumer and producer
It's possible to create one Object and this object will be an atomic variable for this uses the interface java.io.Serializable.
|
Picture 2: create atomic variable
In this new version there are more current collections.
|
BlockingDeque ConcurrentMap ConcurrentNavigableMap |
Picture 3: current Collections
In concurrent process and you would like to lock any resources for other Threads - don't use or access during that time - for this, only use the java.util.concurrent.locks.Lock.
The Executor has the main objective of realizing parallel processes and on a big scale. Now it's possible to use three new interfaces(Executor, ExecutorService, ScheduledExecutorService), These interfaces execute in the Thread pool, so the JVM manages parallel processes. Another improvement is Fork/Join, it helps in a better way for multiple processes and how it can be divided in the letter part and the recursive way.
Conclusion:
This article discusses improvements in the concurrent in java 7, the concurrent package.
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 1600 reads
Welcome java 7 part 3 -NIO 2 JSR 203
Welcome java 7 part 3 -NIO 2 JSR 203
So like the coin project, in the NIO 2 there aren't unprecedented features in this e -specification, but now it's possible do some easier work I/Os in java. With the class java.io.Files are possible to perform several operations in simple mode. For that it needs and uses the java.io.file.Path, this interface represents files and directories in the operating system.
Path path=Paths.get("file.txt");
|
Picture 1: Making a path, the String “file.txt” can be replace for any path in operating system, it's should directories, files, symbolic link and hard link.
|
Picture 2: I/O's operations using the java.nio.Files
It's possible retrieve information in java.io.Path in an easier way, and can get more about one path with six new interfaces.
|
Picture 3: get more with new interfaces
Now in NIO2 read and write in scalable manner is possible with the java.nio.channels.FileChannel, for example some big documents can use three Threads for reading and writing in quick mode.
|
Picture 3: write and read from FileChannel
In Path it is also possible to read and write in a normal way like with the java.io.File.
|
Picture 4: Writing and reading with the Path interface
Conclusion:
This article discusses the package NIO2, showing big features for facilitating I/Os process.
- Login or register to post comments
- Printer-friendly version
- otaviojava's blog
- 2263 reads




Comments
Can we use this in place of any relationl database ...
by javinpaul - 2012-04-17 20:17
Can we use this in place of any relationl database ?
Regards
Javin
The NOSQL has something different than SQL and its objective ...
by otaviojava - 2012-04-18 03:10
The NOSQL has something different than SQL and its objective is different.
See more in:
http://en.wikipedia.org/wiki/NoSQL