Skip to content

Embedded super columns in Cassandra

chhavigangwal edited this page Nov 6, 2013 · 5 revisions

Introduction

In order to add more dimensions to traditional key value (row -column) model of Cassandra, you can create further sub-columns in a column. This can be done by embedding super columns in Cassandra using thrift.

This can be achieved using Kundera. Following is an example :

@Entity
@Table(name = "USER", schema = "KunderaExamples@twissandraTest")
public class UserCassandra
{

    @Id
    @Column(name = "USER_ID")
    private String userId;

    // Embedded object, will persist co-located
    @Embedded
    private PersonalDetailCassandra personalDetail;

    // Embedded object, will persist co-located
    @Embedded
    private ProfessionalDetailCassandra professionalDetail;

 
@Embeddable
@IndexCollection(columns = { @Index(name = "professionId"), @Index(name = "departmentName"),
        @Index(name = "isExceptional"), @Index(name = "age"), @Index(name = "grade"),
        @Index(name = "digitalSignature"), @Index(name = "rating"), @Index(name = "compliance"),
        @Index(name = "height"), @Index(name = "enrolmentDate"), @Index(name = "enrolmentTime"),
        @Index(name = "joiningDateAndTime"), @Index(name = "yearsSpent"), @Index(name = "uniqueId"),
        @Index(name = "monthlySalary"), @Index(name = "birthday"), @Index(name = "birthtime"),
        @Index(name = "anniversary"), @Index(name = "jobAttempts"), @Index(name = "accumulatedWealth"),
        @Index(name = "graduationDay") })
public class ProfessionalDetailCassandra
{
    // Primitive Types
    @Column(name = "PROFESSION_ID")
    private long professionId;

    @Column(name = "DEPARTMENT_NAME")
    private String departmentName;

    @Column(name = "IS_EXCEPTIONAL")
    private boolean isExceptional;

    @Column(name = "AGE")
    private int age;

    @Column(name = "GRADE")
    private char grade; // A,B,C,D,E,F for i to vi

    @Column(name = "DIGITAL_SIGNATURE")
    private byte digitalSignature;

    @Column(name = "RATING")
    private short rating; // 1-10

    @Column(name = "COMPLIANCE")
    private float compliance;

    @Column(name = "HEIGHT")
    private double height;

Cassandra Equivalent :

create column family USER;

Persist an entity using Kundera :

em = emf.createEntityManager();

UserCassandra user1 = new UserCassandra(userId1, "Amresh", "password1", "married");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(1344079067777l));
user1.setProfessionalDetail(new ProfessionalDetailCassandra(1234567, "Labs", false, 31, 'C', (byte) 8,
                (short) 5, (float) 10.0, 163.12, new Date(Long.parseLong("1344079065781")), new Date(Long
                        .parseLong("1344079067623")), new Date(Long.parseLong("1344079069105")), 2, new Long(
                        3634521523423L), new Double(0.23452342343), new java.sql.Date(new Date(Long
                        .parseLong("1344079061111")).getTime()), new java.sql.Time(new Date(Long
                        .parseLong("1344079062222")).getTime()), new java.sql.Timestamp(new Date(Long
                        .parseLong("13440790653333")).getTime()), new BigInteger("123456789"),
                new BigDecimal(123456789), cal));


em.persist(user1);

Data Stored in cassandra :

RowKey: 0001
=> (super_column=PREFERENCE_ID,
     (name=PREFERENCE_ID, value=5031, timestamp=1383739136764))

=> (super_column=professionalDetail,
     (name=ACCUMULATED_WEALTH, value=00000000075bcd15, timestamp=1383739136757)
     (name=AGE, value=0000001f, timestamp=1383739136757)
     (name=ANNIVERSARY, value=00000c396d8d6995, timestamp=1383739136757)
     (name=BIRTH_DAY, value=00000138f15ae077, timestamp=1383739136757)
     (name=BIRTH_TIME, value=00000138f15ae4ce, timestamp=1383739136757)
     (name=COMPLIANCE, value=41200000, timestamp=1383739136757)
     (name=DEPARTMENT_NAME, value=4c616273, timestamp=1383739136757)
     (name=DIGITAL_SIGNATURE, value=0800000000000000, timestamp=1383739136757)
     (name=ENROLMENT_DATE, value=00000138f15af2b5, timestamp=1383739136757)
     (name=ENROLMENT_TIME, value=00000138f15af9e7, timestamp=1383739136757)
     (name=GRADE, value=0043, timestamp=1383739136757)
     (name=GRADUATION_DAY, value=00000138f15afa81, timestamp=1383739136757)
     (name=HEIGHT, value=406463d70a3d70a4, timestamp=1383739136757)
     (name=IS_EXCEPTIONAL, value=00, timestamp=1383739136757)
     (name=JOB_ATTEMPTS, value=075bcd15, timestamp=1383739136757)
     (name=JOINING_DATE_TIME, value=00000138f15affb1, timestamp=1383739136757)
     (name=MONTHLY_SALARY, value=3fce04dd10e38f48, timestamp=1383739136757)
     (name=PROFESSION_ID, value=000000000012d687, timestamp=1383739136757)
     (name=RATING, value=0005, timestamp=1383739136757)
     (name=UNIQUE_ID, value=0000034e3a5d44df, timestamp=1383739136757)
     (name=YEARS_SPENT, value=00000002, timestamp=1383739136757))


Clone this wiki locally