-
Notifications
You must be signed in to change notification settings - Fork 233
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 onwards) 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.
-
Datastores Supported
- Releases
-
Architecture
-
Concepts
-
Getting Started in 5 minutes
-
Features
- Object Mapper
- Polyglot Persistence
- Queries Support
- JPQL (JPA Query Language)
- Native Queries
- Batch insert update
- Schema Generation
- Primary Key Auto generation
- Transaction Management
- REST Based Access
- Geospatial Persistence and Queries
- Graph Database Support
-
Composite Keys
-
No hard annotation for schema
-
Support for Mapped superclass
-
Object to NoSQL Data Mapping
-
Cassandra's User Defined Types and Indexes on Collections
-
Support for aggregation
- Scalar Queries over Cassandra
- Connection pooling using Kundera Cassandra
- Configuration
-
Kundera with Couchdb
-
Kundera with Elasticsearch
-
Kundera with HBase
-
Kundera with Kudu
-
Kundera with RethinkDB
-
Kundera with MongoDB
-
Kundera with OracleNoSQL
-
Kundera with Redis
-
Kundera with Spark
-
Extend Kundera
- Sample Codes and Examples
-
Blogs and Articles
-
Tutorials
* Kundera with Openshift
* Kundera with Play Framework
* Kundera with GWT
* Kundera with JBoss
* Kundera with Spring
-
Performance
-
Troubleshooting
-
FAQ
- Production deployments
- Feedback