Easy-Cassandra User Guide Document version: 005 1
Summary About Easy-Cassandra...5 Features...5 Java Objects Supported...5 About Versions...6 Version: 1.1.0...6 Version: 1.0.9...6 Version: 1.0.8...6 Version: 1.0.7...6 Version: 1.0.6...6 Version: 1.0.5...6 Version: 1.0.4...6 Version: 1.0.3...7 Version: 1.0.2...7 Version: 1.0.1...7 Beyond version...7 For use the Easy-Cassandra is necessary:...7 The Thrift's dependence:...7 Maven:...8 Annotations...10 Used in Class...10 Entity:...10 MappedSuperclass:...10 Used in Field...11 Column:...11 Embedded:...11 Enumerated:...11 Version...12 Index:...12 Id:...12 Complete Example with Annotation...13 Commands...13 Consistency Level...13 Enum Options:...13 ONE:...13 QUORUM...14 ALL...14 DCQUORUM...14 DCQUORUMSYNC...15 The class org.easycassandra.persistence.easycassandramanager...16 Client getclient(string keyspace, String host, int port)...16 Persistence getpersistence(string keyspace, String host, int port)...16 boolean addfamilyobject(class<?> classcolumnfamily)...16 The class org.easycassandra.persistence.persistence...17 Insert Commands...18 insert(object object, ConsistencyLevelCQL consistencylevel)...18 insert(object object)...18 Remove Commands...18 delete(object object)...18 Document version: 005 2
deletebyid(object Id, Class objectclass)...18 Update Commands...18 update(object object, ConsistencyLevel consistencylevel)...19 update(object object, ConsistencyLevel consistencylevel)...19 List Commands...19 findall(class persistenceclass, ConsistencyLevelCQL consistencylevel, int limit)...19 findall(class persistenceclass, ConsistencyLevelCQL consistencylevel)...19 findall(class persistenceclass, int limit)...19 findall(class persistenceclass)...20 findbyindex(object index, Class objectclass, ConsistencyLevelCQL consistencylevelcql, int limit)...20 findbyindex(class persistenceclass, ConsistencyLevelCQL consistencylevel)...20 findbyindex(class persistenceclass, int limit)...20 findbyindex(class persistenceclass)...20 Retrieve from Key row Commands...21 findbykey(object key, Class persistenceclass, ConsistencyLevelCQL consistencylevel)...21 findbykey(object key, Class persistenceclass)...21 findbykeyin(class baseclass,consistencylevelcql consistencylevel,object... keys)...21 findbykeyin(class baseclass, Object... keys)...21 Count Commands...21 count(class<?> clazz,consistencylevelcql consistencylevel)...21 count(class clazz)...22 Sample Abstract DAO...23 Native Commands...25 Get Thrift Client...25 Get Thrift Client in Current Persistence...25 Execute Cassandra Query Language...25 executeupdatecql(string query)...25 executecql(string query)...25 The class org.easycassandra.persistence.jcassandra...26 Add Class for Easy-Cassandra to manager...26 EasyCassandraManager.addFamilyObject(Class<?> classcolumnfamily)...26 Persistence.createJCassandra(String cql)...26 List getresultlist()...26 setfirstresult(int startposition);...27 int getfirstresult();...27 setmaxresults(int maxresult);...27 int getmaxresults();...27 setparameter(string name, Object value);...27 Set<Parameter<?>> getparameters();...28 Parameter<?> getparameter(int position);...28 Parameter<?> getparameter(string name);...28 Object getsingleresult();...28 boolean executeupdate();...28 JPQL in Cassandra Commands...29 Retrieve Objects...29 Select the Object...29 Select some fields in the Object...29 Document version: 005 3
Count number of row in Object...29 Delete Objects...30 remove the Object...30 remove some fields in the Object...30 Update Objects...31 update some fields in the Object...31 How to Contribute with this project...32 Article:...32 Document version: 005 4
About Easy-Cassandra Easy-Cassandra is a framework ORM API and a high client for Apache Cassandra in java, with this is possible persist information from the Java Object in easy way. For this is only necessary add some annotations in some fields and your class. It works like an abstraction's tier in the Thrift, doing call for Cassandra. The Easy-Cassandra uses the Thrift implementation and has like the main objective be one simple ORM (Object relational manager). It need the jdk 7 for run, because some parts in your code was replaced reflection for invoke dynamic. So will have a behavior faster than other framework. The Easy-Cassandra was the first framework compatible with Cassandra version above 0.8 and the first one to use CQL (Cassandra Query Language). For read a class, it use the invokedynamic, fifteen time faster than reflection. Site: https://github.com/otaviojava/easy-cassandra Download: https://github.com/otaviojava/easy-cassandra/downloads Features An ORM easy to use in Cassandra Use the JPQL 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 compatibles 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 Supporting JPA 2.0 annotation Supporting Java Query Language in Cassandra Java Objects Supported The Easy-Cassandra has support for the Object java bellow: 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 Document version: 005 5
java.lang.short java.lang.character java.io.file java.nio.file.path About Versions The current and stable version is 1.1.0 Version: 1.1.0 Client for many Client in sequencial mode Client for many Client in sequencial random DownGrade to java 6 create keyspace automatically do single client Version: 1.0.9 Supporting at jpa 2.0 annotation Query with objects Version to retrieve the timestamp of the object Using the JPQL now supported inheritance Version: 1.0.8 Select Key from 'in' CQL command Create automatically the ColumnFamily in Run Time Create automatically the Index in Run Time Support with cql 2.0 Now, it not necessary create Column Family or Secondary Index. The Easy-Cassandra will create that in run time. 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 Document version: 005 6
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 Column and ColumnFamilyValue in default mode this way its get the field's name Fixes bug in Reflection Beyond version For use the Easy-Cassandra is necessary: JDK 7 or above JPA 2.0 annotation lib Apache Cassandra is running Create the Keystore Do annotations in the class Annotations Realize call for save the object in Cassandra Here Add Easy-Cassandra's Lib https://github.com/otaviojava/easy-cassandra/downloads Add Thrift Dependence: Easy Cassandra with Thrift dependencies The Thrift's dependence: cassandra-thrift-1.0.7.jar commons-codec-1.3.jar commons-lang-2.4.jar commons-logging-1.1.1.jar httpclient-4.0.1.jar httpcore-4.0.1.jar junit-4.4.jar libthrift-0.6.1.jar servlet-api-2.5.jar slf4j-api-1.6.1.jar Document version: 005 7
Maven: For use Easy-Cassandra with maven you must add in your maven application the repository and then add the tag of the project. Document version: 005 8
<project>. <dependencies> <dependency> <groupid>org.easycassandra</groupid> <artifactid>easycassandra</artifactid> <version>1.0.9</version> </dependency> <dependency> <groupid>org.ow2.spec.ee</groupid> <artifactid>ow2-jpa-2.0-spec</artifactid> <version>1.0.11</version> </dependency> <!-- other dependencies --> </dependencies> <repositories> <!-- other repositories --> <repository> <id>easycassandra</id> <url>http://otaviojava.googlecode.com/svn/repository/</url> </repository> </repositories> </project> Document version: 005 9
Annotations The annotations are for mapping of the object, then persist and retrieve the information in Cassandra. In current version are used jpa annotations Used in Class Entity: Annotations for identify the family column name. E.g: @Entity(name = "Person") public class Person implements Serializable {...} @Entity public class Person implements Serializable {...} If this no used the 'name' value, the default value is the Class's name MappedSuperclass: 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 { @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 } Document version: 005 10
Used in Field Column: For identify the column in the Family Column, the classes can be used are: all primitives types (int, long, short, byte, float, double, 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 @Column(name = "year") private Integer year; @Column private Integer year; If this no used the 'name' value, the default value is the Field's name Embedded: The class with this is annotation has fields with Column inside itself, but the persistence way continues in the same Family of the Column. This annotation is to do the object s modeling easily. @Embedded private MyObject myobject; Enumerated: For be used in Enums @Enumerated @Column(name="sex") private EnumClass enumclass; Document version: 005 11
@Enumerated private EnumClass enumclass; If this no used the 'name' value, the default value is the Field's name Version The version annotation is used for know the time that Object was persisted in Cassandra database. It uses the timestamp in column, in other words, this annotation doesn't persist nothing in Cassandra, but retrieves the timestamp from line. This annotation might be java.lang.long, java.lang.string, java.util.date, java.util.calendar. Index: @Version private Date timestampdate; The field with this annotation is an index secundary, can search and retrieve information from the row like Id, need also use the Column together with this annotation. @Index @Column(name = "name") private String name; @Index @Column private String name; Id: The field with this annotation is a 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. @Id private String id; @Id @GeneratedValue private Long id; When you use the auto increment resource the kind of key field must be: java.lang.string, java.lang.long, java.lang.integer, java.lang.byte, java.lang.short, java.math.biginteger The default value for auto equals false, in other words Easy-Cassandra will not generate an auto increment value, when the auto is false can use all type supported by framework and when is auto is true must use Long object. Each Class with FamilyColumn annotation must be this annotation in some field. Document version: 005 12
Complete Example with Annotation @Entity(name = "person") public class Person implements Serializable { private static final long serialversionuid = 3L; @Id private Long id; @Column(name = "name") private String name; @Column(name = "born") private Integer year; //getter and setter } //Embedded class public class Address implements Serializable { private static final long serialversionuid = 1L; @Column(name="state") private String state; @Column(name="cyte") private String city; @Column(name="street") private String street; @Column(name="cep") private String cep; //getter and setter } Commands Consistency Level The org.easycassandra.consistencylevel is an enum that controls both read and write, in Easy-Cassandra, the behavior based on the ReplicationFactor of the keyspace. Enum Options: ONE: Read Will return the record returned by the first replica to respond. A consistency check is always done in a background thread to fix any consistency issues when ConsistencyLevel.ONE is used. This means subsequent calls will have correct data even if the initial read gets an older value. (This is called ReadRepair). Document version: 005 13
Write Ensure that the write has been written to at least 1 replica's commit log and memory table before responding to the client. QUORUM Read Will query all replicas and return the record with the most recent timestamp once it has at least a majority of replicas (N 2 + 1) reported. Again, the remaining replicas will be checked in the background. Write Ensure that the write has been written to N 2 + 1 replicas before responding to the client. ALL Read Will query all replicas and return the record with the most recent timestamp once all replicas have replied. Any unresponsive replicas will fail the operation. Write Ensure that the write is written to all N replicas before responding to the client. Any unresponsive replicas will fail the operation. DCQUORUM Read Returns the record with the most recent timestamp once a majority of replicas within the local datacenter have replied. Write Ensure that the write has been written to <ReplicationFactor> 2 + 1 nodes, within the local datacenter (requires NetworkTopologyStrategy). Document version: 005 14
DCQUORUMSYNC Read Returns the record with the most recent timestamp once a majority of replicas within each datacenter have replied. Write Ensure that the write has been written to <ReplicationFactor> 2 + 1 nodes in each datacenter (requires NetworkTopologyStrategy). Document version: 005 15
The class org.easycassandra.persistence.easycassandramanager This class has objective does connections for Cassandra and manages them, also is it that all information is contained in common between the all objects of the connections, the class Persistence. Client getclient(string keyspace, String host, int port) Method for create the Cassandra's Client native. That: keyspace- name of keyspace in Cassandra host- host that Cassandra be port number of port to communication with Cassandra DataBase EasyCassandraManager.getClient("keyspace", "host", 9160); Persistence getpersistence(string keyspace, String host, int port) Method for create the Easy-Cassandra's Client. That: keyspace- name of keyspace in Cassandra host- host that Cassandra be port number of port to communication with Cassandra Data Base Persistence getpersistence(string keyspace, String host, int port) boolean addfamilyobject(class<?> classcolumnfamily) This command is for add some Class for use in JPQL in Cassandra, so you need add class before its use. EasyCassandraManager.addFamilyObject(Object1.class); EasyCassandraManager.addFamilyObject(Object2.class); Document version: 005 16
The class org.easycassandra.persistence.persistence The class Persistence is the main Class for do call to Cassandra, for created this class you must inform the host, port and Keystore where is Cassandra. Persistence persistence= EasyCassandraManager.getPersistence("javabahia", "localhost", 9160); In current version you can use one or more nodes with easy-cassandra. Using multi-node you have two choice: random or sequential way and both you should add nodes before use the client. For random way, access the client in randomly, use the class PersistenceRandom and for the sequential way, use the client sequentially, use the class PersistenceSequencial. Both are son of Persistence, in other word, it's possible change single node, sequential node and random node in real time. Adding nodes EasyCassandraManager.getPersistence("javabahia", "node1", 9160); EasyCassandraManager.getPersistence("javabahia", "node2", 9160); EasyCassandraManager.getPersistence("javabahia", "node3", 9160); Random Persistence persistence=easycassandramanager.getpersistencerandom("javabahia"); Sequential Persistence persistence=easycassandramanager.getpersistencesequencial("javabahia"); Single Persistence persistence= EasyCassandraManager.getPersistence("javabahia", "localhost", 9160); Document version: 005 17
Insert Commands This are command for persist class in Cassandra. insert(object object, ConsistencyLevelCQL consistencylevel) Insert the object with a consistency level defined, it return true if executed with success. boolean success=persistence.insert(bean,consistencylevelcql.all); insert(object object) Insert the object with a consistency level equal ConsistencyLevelCQL.ONE, it return true if executed with success. boolean success=persistence.insert(bean); Remove Commands delete(object object) Remove all row from key inside of the Object, the key value must be not empty or null. It return true if executed with success. boolean success=persistence.delete(bean); deletebyid(object Id, Class objectclass) Remove all row with key value and use the Class for search the respective Column Family. It return true if executed with success. boolean success=persistence.deletebyid(rowkey, baseclass); Update Commands Document version: 005 18
update(object object, ConsistencyLevel consistencylevel) Update all row with information present in the object with Consistency level sectioned, this command do an override in values in the row, the key value must be not empty or null. It return true if executed with success. boolean success=persistence.update(bean,consistencylevelcql.dcquorum); update(object object, ConsistencyLevel consistencylevel) Update all row with information present in the object consistency level equal ConsistencyLevelCQL.ONE this command do an override in values in the row, the key value must be not empty or null. It return true if executed with success. boolean success=persistence.update(bean); List Commands findall(class persistenceclass, ConsistencyLevelCQL consistencylevel, int limit) Return all objects in the Column Family, recovered by class, with consistency level selected and with a limit number defined. List<BaseObject> list =persistence.findall(baseclass, ConsistencyLevelCQL.DCQUORUMSYNC, 15000); findall(class persistenceclass, ConsistencyLevelCQL consistencylevel) Return all objects in the Column Family, recovered by class, with consistency level selected and with a limit number 10000. List<BaseObject> list =persistence.findall(baseclass, ConsistencyLevelCQL.DCQUORUMSYNC); findall(class persistenceclass, int limit) Return all objects in the Column Family, recovered by class, with consistency level equal ConsistencyLevelCQL.ONE and with a limit number defined. List<BaseObject> list =persistence.findall(baseclass, 5000); Document version: 005 19
findall(class persistenceclass) Return all objects in the Column Family, recovered by class, with consistency level equal ConsistencyLevelCQL.ONE and with a limit number 10000. List<BaseObject> list =persistence.findall(baseclass); findbyindex(object index, Class objectclass, ConsistencyLevelCQL consistencylevelcql, int limit) Return all objects from secondary index in the Column Family, recovered by class, with consistency level selected and with a limit number defined. The class must be some field with Index annotations. List<BaseObject> list =persistence.findbyindex(index,baseclass, ConsistencyLevelCQL.DCQUORUMSYNC, 15000); findbyindex(class persistenceclass, ConsistencyLevelCQL consistencylevel) Return all objects from secondary index in the Column Family, recovered by class, with consistency level selected and with a limit number 10000. The class must be some field with Index annotations. List<BaseObject> list =persistence.findbyindex(index,baseclass, ConsistencyLevelCQL.DCQUORUMSYNC); findbyindex(class persistenceclass, int limit) Return all objects from secondary index in the Column Family, recovered by class, with consistency level equal ConsistencyLevelCQL.ONE and with a limit number defined. The class must be some field with Index annotations. List<BaseObject> list =persistence.findall(index,baseclass, 5000); findbyindex(class persistenceclass) Return all objects from secondary index in the Column Family, recovered by class, with consistency level equal ConsistencyLevelCQL.ONE and with a limit number 10000. The class must be some field with Index annotations. List<BaseObject> list =persistence.findbyindex(index,baseclass); Document version: 005 20
Retrieve from Key row Commands findbykey(object key, Class persistenceclass, ConsistencyLevelCQL consistencylevel) Return the object from key row with the value passed in parameter, recovered by class, with the consistency level selectioned. BaseObject base=(baseobject)persistence.findbykey(idvalue, baseclass,consistencylevelcql.all); findbykey(object key, Class persistenceclass) Return the object from key row with the value passed in parameter, recovered by class, with the consistency level selected. BaseObject base=(baseobject)persistence.findbykey(idvalue, baseclass); findbykeyin(class baseclass,consistencylevelcql consistencylevel,object... keys) Return the object from key row with N value passed in parameter, recovered by class, with the consistency level selected. List<BaseObject> list=persistence.findbykeyin( baseclass,consistencylevelcql.all,param1,param2,param3); findbykeyin(class baseclass, Object... keys) Return the object from key row with N value passed in parameter, recovered by class, with the consistency level equal ConsistencyLevelCQL.ONE. List<BaseObject> list=persistence.findbykeyin( baseclass,param1,param2,param3); Count Commands count(class<?> clazz,consistencylevelcql consistencylevel) Return the number of rows in Column Family with the consistency level selected. Long numberofrow=persistence.count(baseclass,consistencylevelcql.dcquorumsync); Document version: 005 21
count(class clazz) Return the number of rows in Column Family with consistency level equal ConsistencyLevel. Long numberofrow=persistence.count(baseclass); Document version: 005 22
Sample Abstract DAO Bellow a simple example DAO using Easy-Cassandra. public abstract class AbstractDao<T> { private Persistence persistence; private Class baseclass; public AbstractDao(Class<T> baseclass) { this.baseclass=baseclass; persistence = EasyCassandraManager.getPersistence("javabahia", "localhost", 9160); } public boolean insert(t bean) { } return persistence.insert(bean,consistencylevelcql.one); public boolean remove(t bean) { } return persistence.delete(bean); public boolean removefromrowkey(object rowkey) { } return persistence.deletebyid(rowkey, baseclass); public boolean update(t bean) { } return persistence.update(bean); @SuppressWarnings("unchecked") public T retrieve(object id) { return (T) persistence.findbykey(id, baseclass); } @SuppressWarnings("unchecked") public List<T> listall() { } return persistence.findall(baseclass); Document version: 005 23
@SuppressWarnings("unchecked") public List<T> listbyindex(object index) { } return persistence.findbyindex(index, baseclass); public Long count() { return persistence.count(baseclass); } public List<T> findkeyin(object... key) { return persistence.findbykeyin( baseclass,key); } } Document version: 005 24
Native Commands Get Thrift Client If necessary you can call Thrift functions for it, it is necessary inform the host, port, and keystore for the Class EasyCassandraManager. Client client=easycassandramanager.getclient("javabahia", "localhost", 9160); Get Thrift Client in Current Persistence Also is allowed get Thrift's client, used by Persistence. Client client=persistence.getclient(); Execute Cassandra Query Language executeupdatecql(string query) This Command execute the query in String if there was success return true otherwise false boolean success=persistence.executeupdatecql("delete columnname FROM COLUMNFAMILY WHERE KEY = keyname1"); executecql(string query) Retrieve values from Query command and return The values from query like List of the Map<String, String>: Each List is a row. Each Map<String, String>: a key in the map is equal a column's name and the value in the map is the column's value. List<Map<String, String>> result=persistence.executecql("select * from ColumnFamily"); Document version: 005 25
The class org.easycassandra.persistence.jcassandra The interface JCassandra is look like javax.persistence.query in JPA, its main objective is be Java Persistence Query Language, in other words, translate Java's object to Cassandra Data Base. But before talk about that call, is very important say that there are some different features between NOSQL Column Family and SQL. The first on is there aren't relationship between Column family. So you might not do relationship. Like one to one, one to many or many to many. The second one the Cassandra is case sensitive, then Person, person and person are different names in Cassandra. Add Class for Easy-Cassandra to manager The first thing to do for use JPQL in Easy-Cassandra is add Class, for this you don't need use XML, that's it without XML. EasyCassandraManager.addFamilyObject(Class<?> classcolumnfamily) This command is for add some Class for use in JPQL in Cassandra, so you need add class before its use. EasyCassandraManager.addFamilyObject(Object1.class); EasyCassandraManager.addFamilyObject(Object2.class); Persistence.createJCassandra(String cql) Method for create the interface JCassandra that is for used to control query execution. JCassandra query=persistence.createjcassandra("select * from Object"); List getresultlist() Execute a CQL and return the result results as a List. JCassandra query=persistence.createjcassandra("select * from Object"); List<Person> persons=jcassandra.getresultlist(); Document version: 005 26
setfirstresult(int startposition); Set the position of the first result to retrieve. JCassandra query=persistence.createjcassandra("select * from Object"); jcassandra.setfirstresult(2); List<Person> persons=jcassandra.getresultlist(); int getfirstresult(); Get the parameter objects corresponding to the declared parameters of the query. Returns empty set if the query has no parameters. This method is not required to be supported for native queries. JCassandra query=persistence.createjcassandra("select * from Object"); jcassandra.getfirstresult(); setmaxresults(int maxresult); Set the maximum number of results to retrieve. JCassandra query=persistence.createjcassandra("select * from Object"); jcassandra.setmaxresults(2); List<Person> persons=jcassandra.getresultlist(); int getmaxresults(); The position of the first result the query object was set to retrieve. Returns 0 if setfirstresult was not applied to the query object. JCassandra query=persistence.createjcassandra("select * from Object"); jcassandra.getmaxresults(); setparameter(string name, Object value); Bind an argument to a named parameter. JCassandra jcassandra=persistence.createjcassandra("select * from Person where id =:id "); jcassandra.setparameter("id","10"); Document version: 005 27
Set<Parameter<?>> getparameters(); Get the parameter objects corresponding to the declared parameters of the query. Returns empty set if the query has no parameters. This method is not required to be supported for native queries. JCassandra jcassandra=persistence.createjcassandra("select * from Person where id =:id and name =:name"); Set<Parameter<?>> setparameters=jcassandra.getparameters(); Parameter<?> getparameter(int position); Get the parameter object corresponding to the declared positional parameter with the given position. This method is not required to be supported for native queries. JCassandra jcassandra=persistence.createjcassandra("select * from Person where id =:id and name =:name"); Parameter<?> parameter=jcassandra.getparameter(0); Parameter<?> getparameter(string name); Get the parameter object corresponding to the declared parameter of the given name. This method is not required to be supported for native queries. JCassandra jcassandra=persistence.createjcassandra("select * from Person where id =:id and name =:name"); Parameter<?> parameter=jcassandra.getparameter( name ); Object getsingleresult(); Execute a cql that returns a single result. JCassandra jcassandra=persistence.createjcassandra("select * from Person where id = '31' "); Person person=(person)jcassandra.getsingleresult(); boolean executeupdate(); Execute an update or delete statement. JCassandra jcassandra=persistence.createjcassandra("delete from Person where id =:id "); jcassandra.setparameter("id", 1l); jcassandra.executeupdate(); Document version: 005 28
JPQL in Cassandra Commands Retrieve Objects Select the Object For retrieve the object in CQL command, you should use the '*' character. You might use the where condition in select, for this the field should be index secondary and key. JCassandra jcassandra=persistence.createjcassandra("select * from Person"); List<Person> persons=jcassandra.getresultlist(); Select some fields in the Object You are able select some fields, for this separate the fields with comma. You might use the where condition in select, for this the field should be index secondary and key. JCassandra jcassandra=persistence.createjcassandra("select name, id, year from Person"); List<Map<String, Object>> list=jcassandra.getresultlist(); Count number of row in Object Count number of row in Column Family use the count(*) command. You might use the where condition in select, for this the field should be index secondary and key. JCassandra jcassandra=persistence.createjcassandra("select count(*) from Person "); Long counter=(long)jcassandra.getsingleresult(); Document version: 005 29
Delete Objects remove the Object For remove the object in Cassandra, you need know the key and use this in 'where' condition. JCassandra jcassandra=persistence.createjcassandra("delete from Person where id =:id "); jcassandra.setparameter("id", 1l); jcassandra.executeupdate(); remove some fields in the Object You are able remove some fields, for this separate the fields with comma, you need know the key use that in 'where' condition. JCassandra jcassandra=persistence.createjcassandra("delete name from Person where id =:id "); jcassandra.setparameter("id", 10l); jcassandra.executeupdate(); Document version: 005 30
Update Objects update some fields in the Object You are able update some fields, for this separate the fields with comma, you need know the key use that in 'where' condition. JCassandra jcassandra=persistence.createjcassandra("update Person set name =:name where id =:id "); jcassandra.setparameter("id", 4l); jcassandra.setparameter("name", "Otavio Santana"); jcassandra.executeupdate(); Document version: 005 31
How to Contribute with this project If you would like help the project, report software error, fix bug in the document you can send email for: otaviojava@java.net or participate in Google Groups: https://groups.google.com/group/easy-cassandra Article: http://weblogs.java.net/blog/otaviojava/archive/2012/01/24/persisting-information-cassandra-java-simple-exa mple http://weblogs.java.net/blog/otaviojava/archive/2012/02/01/persist-document-cassandra Document version: 005 32