Skip to content

Secondary Index Mapping in Cassandra

chhavigangwal edited this page Nov 6, 2013 · 5 revisions

Introduction :

Cassandra supports secondary indexes of the type KEYS (similar to a hash index). Secondary indexes can be used for querying and applying filters on columns other than primary indexes.

Secondary indexes can be created using Cassandra only if ddl.auto.prepare property is enabled.

<property name="kundera.ddl.auto.prepare" value="create" />
@Entity
@Table(name="property")
@IndexCollection(columns = { @Index(name = "likedBy"), @Index(name = "income"), @Index(name = "settlementDate"),
        @Index(name = "dateSet"), @Index(name = "and"), @Index(name = "between"), @Index(name = "or") })
public class Property
{
    @Id
    private String id;

    @Column
    private String likedBy;

    @Column
    private String income;

    @Column
    private String settlementDate;

    @Column
    private String dateSet;

    @Column
    private String and;

    @Column
    private String between;

    @Column
    private String or;

    @Column
    private String set;

An equivalent created in Cassandra for secondary the given object :

CREATE TABLE property (
  key text PRIMARY KEY,
  "and" text,
  between text,
  "dateSet" text,
  income text,
  "likedBy" text,
  or text,
  "set" text,
  "settlementDate" text
);

CREATE INDEX property_and_idx ON property ("and");
CREATE INDEX property_between_idx ON property (between);
CREATE INDEX property_dateSet_idx ON property ("dateSet");
CREATE INDEX property_income_idx ON property (income);
CREATE INDEX property_likedBy_idx ON property ("likedBy");
CREATE INDEX property_or_idx ON property (or);
CREATE INDEX property_settlementDate_idx ON property ("settlementDate");

These indexes can be queried using Kundera in the following manner :

 // Select query.
 List<Property> entityWithClauses = em.createQuery(
                "select t from Property t where t.income=1233").getResultList();
       
 entityWithClauses = em.createQuery("select t from Property t where t.likedBy=kk").getResultList();
       
 entityWithClauses = em.createQuery("select t from Property t where t.dateSet=dateSet").getResultList();
       
 entityWithClauses = em.createQuery("select t from Property t where t.settlementDate=12/12/12")
                .getResultList();

 entityWithClauses = em.createQuery("select t from Property t where t.or=or").getResultList();
      
 entityWithClauses = em.createQuery("select t from Property t where t.between=between").getResultList();
      
 entityWithClauses = em.createQuery("select t from Property t where t.and=and").getResultList();
      
Clone this wiki locally