-
Notifications
You must be signed in to change notification settings - Fork 0
Using Compound keys with Kundera
Unstructured Data? Scalability is not an issue, use NOSQL! Compound keys is one way to achieve faster search over denormalized data. Kundera(2.2 release on-wards) supports compound key.
You can define compound key with entity definition using @EmbeddedId annotation, like:
@Entity
@Table(name="User", schema="KunderaExamples@mongoTest")
public class PrimeUser
{
@EmbeddedId
private CompoundKey key;
@Column
private String nonKeyColumn;
Here key has to be an Embeddable entity:
@Embeddable
public class CompoundKey
{
@Column private String userId;
@Column private int tweetId;
@Column private UUID timeLineId;
Here CompoundKey is essentially holding definition for composite/compound key for Cassandra or other NOSQls. And we need to define other non row key columns with Primary entity(holding reference of CompoundKey with @EmbeddedId).
Cassandra provides support of composite key via CQL3.0. To use Kundera with composite key support over Cassandra, we need to make sure order of fields defined in embeddable entity(e.g. CompoundKey) should be same as while creating column family script. For example:
CREATE TABLE users (
user_id varchar,
first_name varchar,
last_name varchar,
city varchar,
PRIMARY KEY (user_id, first_name));
Here order is user_id, first_name. user_id can be termed as partition key and first_name is "cluster/remaining key".
So to ensure ordering, while defining embeddable entity within Kundera, it must be in same order. For example:
@Embeddable
public class CassandraCompoundKey
{
@Column private String user_id,; ====> {partition key}
@Column private String first_name; ====> {cluster/remaining key}
/**
*
*/
public CassandraCompoundKey()
{
}
// setter and getters
Kundera ensures case sensitivity of CQL columns and column family name. For example, If column name is declared as:
private String userId;
OR
@Column(name="userId")
private String userid;
Then, please make sure that while creating CQL column family you must ensure same. e.g.:
CREATE TABLE users (
"userId" varchar, <========== enclose userId within ""
first_name varchar,
last_name varchar,
city varchar,
PRIMARY KEY (user_id, first_name));
EntityManagerFactory emf = Persistence.createEntityManagerFactory("cassandra_pu");
EntityManager em = emf.createEntityManager();
em.setProperty("cql.version", "3.0.0");
Performing any CRUD operation is similar the way, for single primary key.
You can refer here for examples:
**Note:**Current trunk branch supports it for MongoDB and cassandra. Feature is available with release 2.2 onwards.