Cassandra DataStax: Developer Guide With Spring Data Cassandra
Take a look at a tutorial on Cassandra.
Join the DZone community and get the full member experience.
Join For FreeI did this POC when the latest version was Spring 4.x. Please check the latest version of Cassandra and Spring. We will discuss a Cassandra implementation.
- http://www.datastax.com/documentation/cassandra/2.0/cassandra/gettingStartedCassandraIntro.html
- Recommended stable production version — DataStax Enterprise 4.5. (When this article was written).
- Compound Partition and Clustered keys: http://www.datastax.com/documentation/cql/3.0/cql/ddl/ddl_compound_keys_c.html
- Spring-Data-Cassandra API and reference docs: http://projects.spring.io/spring-data-cassandra/
Download and Installation
1. Tarball Installation
DataStax DB
- You need to register yourself with DataStax for the download.
- DataStax Enterprise — http://www.datastax.com/download#dl-enterprise.
- Create these folders and permissions:
mkdir -p /var/log/cassandrasudo
sudo chmod 777 /var/log/cassandrasudo
mkdir -p /var/lib/cassandra/datasudo
chmod 777 /var/lib/cassandra/datasudo
mkdir -p /var/lib/cassandra/commitlogsudo
chmod 777 /var/lib/cassandra/commitlogsudo
mkdir -p /var/lib/cassandra/saved_cachessudo
chmod 777 /var/lib/cassandra/saved_caches
- How to run Cassandra: Go to the DataStax Cassandra installed folder on Mac/Linux/Unix env:
cd /Users/<userName>/dse-<version>/bin 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 |
user id |
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 the 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.
- user id
- id of the user who made the change
- action
- SAVE DRAFT, 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 |
user id |
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:
- 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:
2. Package Installation:
DataStax All-in-One Installer
- You need to register with DataStax for DataStax All-in-One Installer download.
- DataStax Enterprise — http://www.datastax.com/download#dl-dseinstaller
- Installation: http://www.datastax.com/documentation/getting_started/doc/getting_started/gsInstallNonRoot.html
- Follow the installation steps given in above link and install.
- Once your Installation is completed, your DataStax will be in the following location:
/users/dse
How to Run DataStax?
- Cassandra: Go to your /Users/dse via Terminal and execute the following command. This will start Cassandra.
sudo ./bin/dse cassandra -f
Opscenter and DataStax-Agent:
- Go to /Users//dse in new Tab and execute the command. This will start the opscenter
sudo ./opscenter/bin/opscenter -f
- Go to /Users//dse in new Tab and execute the command. This will start the datastax-agent
sudo ./datastax-agent/bin/datastax-agent -f
Using DataStax:
- Now you can see the Opscenter in Browser in the following address: localhost:8888/
- Here, you are able to see your Cassandra cluster visually (kind of a monitoring tool for Cassandra).
- You need to start DevCenter — which came with DataStax (you can find the shortcut for DevCenter in your Desktop), if you want to query and access the cassandra DB.
- Getting Started with DevCenter: http://www.datastax.com/what-we-offer/products-services/devcenter
Integrate Cassandra With Spring Data Cassandra:
/**
* 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
}
}
3. Data Modeling
a. Primary/Clustered/Partitioned 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;
b. Repository
/**
* Created by: Rajiv Srivastava*/
@Repository
public interface AuditRepository extends CrudRepository <EventAudit, EventAuditKey> {
@Query(select * from event_owner.event_audit)
public List<EventAudit&> eventAudit();
}
c. 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;
}
}
Join us in #cassandra on irc.freenode.net and ask questions.
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.”
Published at DZone with permission of Rajiv Srivastava. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments