Cassandra DataStax – Developer Guide with Spring Data Cassandra


We will discus Cassandra implementation :

Important Points:

Download and Installation:

  1. Tarball Installation

  1. DataStax DB
    • You need to register yourself with DataStax for download.
    • DataStax Enterprise – http://www.datastax.com/download#dl-enterprise.
    • Create These folders and changed permission:
    • sudo chmod 777 /var/log/cassandra
      sudo mkdir -p /var/lib/cassandra/data
      sudo chmod 777 /var/lib/cassandra/data
      sudo mkdir -p /var/lib/cassandra/commitlog
      sudo chmod 777 /var/lib/cassandra/commitlog
      sudo mkdir -p /var/lib/cassandra/saved_caches
      sudo chmod 777 /var/lib/cassandra/saved_caches
    • How to run Cassandra: Go to DataStax Cassndra installed folder on Mac/Linux/Unix env:                                                                                          
                                                                                                                                                                        cd /Users/<userName>/dse-4.5.2/bin</pre>
      <pre>sudo ./dse cassandra -f
       
      //This above command Cassandra DB on your local system. Hit enter to quit from ruining server in background and start CQL query console.
      sudo ./cqlsh
      
  • Create Schema:
    CREATE SCHEMA event_owner WITH replication = {‘class’: ‘SimpleStrategy’, ‘replication_factor’ : 1 };
    1. Schema(Keyspace) name: event_owner
    2. Table name: event_audit
    Table creation syntax:Please find the revised data model and details below.Note: I have used expanded names for easier understanding which can be shortened later on.
    CREATE TABLE  event_owner.event_audit (
    ctg    text,
    month       timestamp,
    ctgid      text,
    ver     timeuuid,
    userid      text,
    action      text,
    content     text,
    PRIMARY KEY ((category,month),cat_id,version)
    ) WITH CLUSTERING ORDER BY (cat_id ASC, version DESC);
    Sample Data
  • category | month                    | cat_id | version                              | action | content      | userid
    ———-+————————–+——–+————————————–+——–+————–+———–
    CC | 2014-01-01 05:30:00+0530 |   8000 | b3fc48e0-5608-11e4-aacd-e700f669bcfc |  DRAFT | json content | 155045940
    CC | 2014-01-01 05:30:00+0530 |   9000 | a4747460-5608-11e4-aacd-e700f669bcfc |  DRAFT | json content | 155045940
    Description

    • category
      • Commitcode / Part Association/ EventTag [CC / PA / ET]
    • month
      • 12 AM timestamp of first day of the month the change is made
    • cat_id 
      • Unique id for a particular category [Say inc axe of commit code it is cc_id say 9000]
    • version 
      • it is the unique id that indicates the version number. 
      • You can populate it using now() function. 
      • It has an embedded timestamp that can be used to know the timestamp. Use dateOf() function to get the timestamp value.
    • userid
      • id of the user who made the change
    • action 
      • SAVEDRAFT/ PUBLISH / DELETE etc.
    • content 
      • actual json content after the change [full json]

  • Sample Query to access the data
    cqlsh:cdb> select * from audit where category=’CC’ and month=’2014-01-01 05:30:00+0530′ and cat_id=’9000′;
    category | month                    | cat_id | version                              | action | content      | userid
    ———-+————————–+——–+————————————–+——–+————–+———–
    CC | 2014-01-01 05:30:00+0530 |   9000 | a4747460-5608-11e4-aacd-e700f669bcfc |  DRAFT | json content | 155045940
  • SQL Like Commands: You can use same standard SQL DDL/DML commands/syntax for Cassandra query, like-
    • Drop table 
    • Update
    • Delete
    • Truncate
    • Select query

    2. DataStax OpsCenter- 

http://www.datastax.com/what-we-offer/products-services/datastax-opscenter

    3. DataStax DevCenter

        Installation: DataStax DevCenter is a visual CQL query tool for Cassandra and DataStax Enterprise.

        How to start OpsCenter GUI: http://www.datastax.com/documentation/getting_started/doc/getting_started/gettingStartedInstallOpscTar_t.html

2. Package Installation:

DataStax All-in-One Installer

How to run DataStax?

  • Cassandra:
    • Go to your /Users//dse via Terminal and execute the following command:
      • sudo ./bin/dse cassandra -f (This will start the cassandra)
  • Opscenter and DataStax-Agent:
    • Go to /Users//dse in new Tab and execute command : 
      • sudo ./opscenter/bin/opscenter -f (This will start the opscenter)
    • Go to /Users//dse in new Tab and execute command :
      • sudo ./datastax-agent/bin/datastax-agent -f (This will start the datastax-agent)

Using DataStax:

  • Now you can see the Opscenter in Browser in following address:
    • localhost:8888/
  • Here You can able to see the your Cassandra cluster visually, (Kind of monitoring tool for cassandra)

 

Integrate Cassandra with Spring Data Cassandra :

  1. /**
     * Created by: Rajiv Srivastava
     */
    @Configuration
    @ComponentScan(basePackages = {com.cassandraproject.dao,com.cassandraproject.utils})
    @EnableCassandraRepositories(basePackages ={com.cassandraproject.repository})
    public class AuditCoreContextConfig extends AbstractCassandraConfiguration {
    
     @Override
     protected String getKeyspaceName() {
     return event_owner; //Schema or Keyspace name
     }
    
     @Override
     protected String getContactPoints() {
     return localhost;//IP address of server/local machine. Host of a clusters can be separated with comma (,) like host1,host2. Also minimum two host should be added, so that second Cassandra server will be connected if first is down.
     }
    
     @Override
     protected int getPort() {
     return 9042; //Cassandra DB port
     }
    }
    

2. Data Modelling

 a. Primary/Clustered/Partioned key


/**
 * Created by: Rajiv Srivastava
 */

/* Keyspac/Schema- event_owner
 * CREATE TABLE event_owner.event_audit (
 ctg text,
 month timestamp,
 ctgid text,
 ver timeuuid,
 userid text,
 action text,
 content text,
 PRIMARY KEY ((ctg,month),ctgid,ver)
 )WITH CLUSTERING ORDER BY (ctgid ASC, ver DESC);

*/

@PrimaryKeyClass
public class EventAuditKey implements Serializable {

private static final long serialVersionUID = 1L;

@PrimaryKeyColumn(name = ctg, ordinal = 0, type = PrimaryKeyType.PARTITIONED)
 private String category;

 @PrimaryKeyColumn(name = month, ordinal = 1, type = PrimaryKeyType.PARTITIONED)
 private Date month;

 @PrimaryKeyColumn(name = ctgid, ordinal = 2, type = PrimaryKeyType.CLUSTERED, ordering =Ordering.ASCENDING)
 private String categoryId;

 @PrimaryKeyColumn(name = ver, ordinal = 3, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
 private UUID version;

 3. Repository


/**
 * Created by: Rajiv Srivastava*/
@Repository
public interface AuditRepository extends CrudRepository <EventAudit, EventAuditKey> {

	@Query(select * from event_owner.event_audit)
	public List<EventAudit&amp> eventAudit();
}

4. CRUD Operation using JPA/CrudRepository – DAO Layer


/**
 * Created by: Rajiv Srivastava*/
@Component
public class EventDaoImpl implements EventAuditDao {

 @Autowired
 public AuditRepository auditRepository;

 @Override
 public void save(EventAudit entity) {
 auditRepository.save(entity);

 }

 @Override
 public void save(Collection<EventAudit> entities) {
 auditRepository.save(entities);
 }

 @Override
 public EventAudit find(EventAuditKey eventAuditKey) {
 if(null==eventAuditKey){
 throw new IllegalArgumentException(It doesn't has all required instance variable set);
 }
 return auditRepository.findOne(eventAuditKey);
 }

 @Override
 public List<EventAudit> getAll() {

 Iterable<EventAudit> iterable=auditRepository.findAll();
 if(null != iterable.iterator()){
 return Lists.newArrayList(iterable.iterator());
 }
 return new ArrayList<>();
 }

 @Override
 public List<EventAudit> getListEventAuditMonthCategoryWise(Date date, String Category) {
 // TODO Auto-generated method stub
 return null;
 }
}

Apache Cassandra: 

  * Getting started: http://wiki.apache.org/cassandra/GettingStarted

  * Join us in #cassandra on irc.freenode.net and ask questions

  * Subscribe to the Users mailing list by sending a mail to

    user-subscribe@cassandra.apache.org

  * Planet Cassandra aggregates Cassandra articles and news:

    http://planetcassandra.org/

For more on what commands are supported by CQL, see

https://github.com/apache/cassandra/blob/trunk/doc/cql3/CQL.textile.  A

reasonable way to think of it is as, “SQL minus joins and subqueries.”

Advertisements

2 thoughts on “Cassandra DataStax – Developer Guide with Spring Data Cassandra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s