Spring Boot and RESTful API(四)Cassandra Spring Data
Spring Boot and RESTful API(4)Cassandra Spring Data
Spring Boot and RESTful API(4)Cassandra Spring Data
Possible Data Structure
>CREATE KEYSPACE jobsmonitor WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
>use jobsmonitor;
>CREATE TABLE jobcounthistory(
source_id text,
record_date TIMESTAMP,
new_active_count int,
new_admin_count int,
old_active_count int,
old_admin_count int,
PRIMARY KEY ( source_id, record_date )
) WITH CLUSTERING ORDER BY (record_date DESC);
Possible query is as follow:
select * from jobcounthistory where source_id = 'asdfasf';
select * from jobcounthistory where source_id = 'asdf' and record_date > '2017-06-11';
>CREATE TABLE jobcountdiff(
date text,
diff int,
record_date TIMESTAMP,
source_id text,
PRIMARY KEY ( date, diff, record_date )
) WITH CLUSTERING ORDER BY (diff ASC, record_date DESC);
Possible Query is as follow:
select * from jobcountdiff where date = '2017-06-15';
select * from jobcountdiff where date = '2017-06-15' and diff > 10;
Code and Setup
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
Configuration in application.yaml
logging:
level:
debug
spring:
profiles:
active:
dev
data:
solr:
host:
http://www.wwaattssuunn.com/localhost:8983/solr/
cassandra:
keyspace-name:jobsmonitor
contact-points: localhost
Domain Class
package com.sillycat.jobsmonitorapi.domain;
import java.util.Date;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
@Table("jobcounthistory")
public class JobCountHistory {
@PrimaryKeyColumn(name = "source_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String sourceID;
@PrimaryKeyColumn(name = "record_date", ordinal = 1, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
private Date recordDate;
@Column("new_active_count")
private Integer newActiveCount;
@Column("new_admin_count")
private Integer newAdminCount;
@Column("old_active_count")
private Integer oldActiveCount;
@Column("old_admin_count")
private Integer oldAdminCount;
public JobCountHistory() {
}
public String getSourceID() {
return sourceID;
}
public void setSourceID(String sourceID) {
this.sourceID = sourceID;
}
public Date getRecordDate() {
return recordDate;
}
public void setRecordDate(Date recordDate) {
this.recordDate = recordDate;
}
public Integer getNewActiveCount() {
return newActiveCount;
}
public void setNewActiveCount(Integer newActiveCount) {
this.newActiveCount = newActiveCount;
}
public Integer getNewAdminCount() {
return newAdminCount;
}
public void setNewAdminCount(Integer newAdminCount) {
this.newAdminCount = newAdminCount;
}
public Integer getOldActiveCount() {
return oldActiveCount;
}
public void setOldActiveCount(Integer oldActiveCount) {
this.oldActiveCount = oldActiveCount;
}
public Integer getOldAdminCount() {
return oldAdminCount;
}
public void setOldAdminCount(Integer oldAdminCount) {
this.oldAdminCount = oldAdminCount;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Repository Interface
package com.sillycat.jobsmonitorapi.repository;
import java.util.List;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.sillycat.jobsmonitorapi.domain.JobCountHistory;
public interface JobCountHistoryRepositoryCassandra extends CrudRepository<JobCountHistory, String> {
@Query("select * from jobcounthistory where source_id=?0")
public List<JobCountHistory> findBySourceID(String sourceID);
@Query("delete from jobcounthistory where source_id=?0")
public void deleteBySourceID(String sourceID);
}
Unit Test Class
package
com.sillycat.jobsmonitorapi.repository;
import java.util.Date;import
java.util.List;
import org.apache.http.util.Asserts;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import
org.springframework.test.context.junit4.SpringRunner;
import
com.sillycat.jobsmonitorapi.domain.JobCountHistory;
@RunWith(SpringRunner.class)@SpringBootTest
public class
JobCountHistoryCassandraTest {
@Autowired
JobCountHistoryRepositoryCassandra jobCountHistoryRepositoryCassandra;
@Test
public void save() throws Exception {
jobCountHistoryRepositoryCassandra.deleteBySourceID("9527");
JobCountHistory item1 = new JobCountHistory();
item1.setNewActiveCount(1);
item1.setNewAdminCount(12);
item1.setOldActiveCount(12);
item1.setOldAdminCount(1);
item1.setSourceID("9527");
item1.setRecordDate(new Date());
jobCountHistoryRepositoryCassandra.save(item1);
List<JobCountHistory> result = jobCountHistoryRepositoryCassandra.findBySourceID("9527");
Asserts.notNull(result, "result is not null");
}
}
References:
http://sillycat.iteye.com/blog/2383513
Spring Boot and RESTful API(4)Cassandra Spring Data
Possible Data Structure
>CREATE KEYSPACE jobsmonitor WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
>use jobsmonitor;
>CREATE TABLE jobcounthistory(
source_id text,
record_date TIMESTAMP,
new_active_count int,
new_admin_count int,
old_active_count int,
old_admin_count int,
PRIMARY KEY ( source_id, record_date )
) WITH CLUSTERING ORDER BY (record_date DESC);
Possible query is as follow:
select * from jobcounthistory where source_id = 'asdfasf';
select * from jobcounthistory where source_id = 'asdf' and record_date > '2017-06-11';
>CREATE TABLE jobcountdiff(
date text,
diff int,
record_date TIMESTAMP,
source_id text,
PRIMARY KEY ( date, diff, record_date )
) WITH CLUSTERING ORDER BY (diff ASC, record_date DESC);
Possible Query is as follow:
select * from jobcountdiff where date = '2017-06-15';
select * from jobcountdiff where date = '2017-06-15' and diff > 10;
Code and Setup
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
Configuration in application.yaml
logging:
level:
debug
spring:
profiles:
active:
dev
data:
solr:
host:
http://www.wwaattssuunn.com/localhost:8983/solr/
cassandra:
keyspace-name:jobsmonitor
contact-points: localhost
Domain Class
package com.sillycat.jobsmonitorapi.domain;
import java.util.Date;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.cassandra.core.Ordering;
import org.springframework.cassandra.core.PrimaryKeyType;
import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKeyColumn;
import org.springframework.data.cassandra.mapping.Table;
@Table("jobcounthistory")
public class JobCountHistory {
@PrimaryKeyColumn(name = "source_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED)
private String sourceID;
@PrimaryKeyColumn(name = "record_date", ordinal = 1, type = PrimaryKeyType.CLUSTERED, ordering = Ordering.DESCENDING)
private Date recordDate;
@Column("new_active_count")
private Integer newActiveCount;
@Column("new_admin_count")
private Integer newAdminCount;
@Column("old_active_count")
private Integer oldActiveCount;
@Column("old_admin_count")
private Integer oldAdminCount;
public JobCountHistory() {
}
public String getSourceID() {
return sourceID;
}
public void setSourceID(String sourceID) {
this.sourceID = sourceID;
}
public Date getRecordDate() {
return recordDate;
}
public void setRecordDate(Date recordDate) {
this.recordDate = recordDate;
}
public Integer getNewActiveCount() {
return newActiveCount;
}
public void setNewActiveCount(Integer newActiveCount) {
this.newActiveCount = newActiveCount;
}
public Integer getNewAdminCount() {
return newAdminCount;
}
public void setNewAdminCount(Integer newAdminCount) {
this.newAdminCount = newAdminCount;
}
public Integer getOldActiveCount() {
return oldActiveCount;
}
public void setOldActiveCount(Integer oldActiveCount) {
this.oldActiveCount = oldActiveCount;
}
public Integer getOldAdminCount() {
return oldAdminCount;
}
public void setOldAdminCount(Integer oldAdminCount) {
this.oldAdminCount = oldAdminCount;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Repository Interface
package com.sillycat.jobsmonitorapi.repository;
import java.util.List;
import org.springframework.data.cassandra.repository.Query;
import org.springframework.data.repository.CrudRepository;
import com.sillycat.jobsmonitorapi.domain.JobCountHistory;
public interface JobCountHistoryRepositoryCassandra extends CrudRepository<JobCountHistory, String> {
@Query("select * from jobcounthistory where source_id=?0")
public List<JobCountHistory> findBySourceID(String sourceID);
@Query("delete from jobcounthistory where source_id=?0")
public void deleteBySourceID(String sourceID);
}
Unit Test Class
package
com.sillycat.jobsmonitorapi.repository;
import java.util.Date;import
java.util.List;
import org.apache.http.util.Asserts;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import
org.springframework.test.context.junit4.SpringRunner;
import
com.sillycat.jobsmonitorapi.domain.JobCountHistory;
@RunWith(SpringRunner.class)@SpringBootTest
public class
JobCountHistoryCassandraTest {
@Autowired
JobCountHistoryRepositoryCassandra jobCountHistoryRepositoryCassandra;
@Test
public void save() throws Exception {
jobCountHistoryRepositoryCassandra.deleteBySourceID("9527");
JobCountHistory item1 = new JobCountHistory();
item1.setNewActiveCount(1);
item1.setNewAdminCount(12);
item1.setOldActiveCount(12);
item1.setOldAdminCount(1);
item1.setSourceID("9527");
item1.setRecordDate(new Date());
jobCountHistoryRepositoryCassandra.save(item1);
List<JobCountHistory> result = jobCountHistoryRepositoryCassandra.findBySourceID("9527");
Asserts.notNull(result, "result is not null");
}
}
References:
http://sillycat.iteye.com/blog/2383513