Spring Boot and RESTful API(4)Cassandra Spring Data

SpringBootandRESTfulAPI(4)CassandraSpringData

PossibleDataStructure

>CREATEKEYSPACEjobsmonitorWITHREPLICATION={'class':'SimpleStrategy','replication_factor':1};

>usejobsmonitor;

>CREATETABLEjobcounthistory(

source_idtext,

record_dateTIMESTAMP,

new_active_countint,

new_admin_countint,

old_active_countint,

old_admin_countint,

PRIMARYKEY(source_id,record_date)

)WITHCLUSTERINGORDERBY(record_dateDESC);

Possiblequeryisasfollow:

select*fromjobcounthistorywheresource_id='asdfasf';

select*fromjobcounthistorywheresource_id='asdf'andrecord_date>'2017-06-11';

>CREATETABLEjobcountdiff(

datetext,

diffint,

record_dateTIMESTAMP,

source_idtext,

PRIMARYKEY(date,diff,record_date)

)WITHCLUSTERINGORDERBY(diffASC,record_dateDESC);

PossibleQueryisasfollow:

select*fromjobcountdiffwheredate='2017-06-15';

select*fromjobcountdiffwheredate='2017-06-15'anddiff>10;

CodeandSetup

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-cassandra</artifactId>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-jdbc</artifactId>

</dependency>

Configurationinapplication.yaml

logging:

level:

debug

spring:

profiles:

active:

dev

data:

solr:

host:

http://localhost:8983/solr/

cassandra:

keyspace-name:jobsmonitor

contact-points:localhost

DomainClass

packagecom.sillycat.jobsmonitorapi.domain;

importjava.util.Date;

importorg.apache.commons.lang3.builder.ToStringBuilder;

importorg.springframework.cassandra.core.Ordering;

importorg.springframework.cassandra.core.PrimaryKeyType;

importorg.springframework.data.cassandra.mapping.Column;

importorg.springframework.data.cassandra.mapping.PrimaryKeyColumn;

importorg.springframework.data.cassandra.mapping.Table;

@Table("jobcounthistory")

publicclassJobCountHistory{

@PrimaryKeyColumn(name="source_id",ordinal=0,type=PrimaryKeyType.PARTITIONED)

privateStringsourceID;

@PrimaryKeyColumn(name="record_date",ordinal=1,type=PrimaryKeyType.CLUSTERED,ordering=Ordering.DESCENDING)

privateDaterecordDate;

@Column("new_active_count")

privateIntegernewActiveCount;

@Column("new_admin_count")

privateIntegernewAdminCount;

@Column("old_active_count")

privateIntegeroldActiveCount;

@Column("old_admin_count")

privateIntegeroldAdminCount;

publicJobCountHistory(){

}

publicStringgetSourceID(){

returnsourceID;

}

publicvoidsetSourceID(StringsourceID){

this.sourceID=sourceID;

}

publicDategetRecordDate(){

returnrecordDate;

}

publicvoidsetRecordDate(DaterecordDate){

this.recordDate=recordDate;

}

publicIntegergetNewActiveCount(){

returnnewActiveCount;

}

publicvoidsetNewActiveCount(IntegernewActiveCount){

this.newActiveCount=newActiveCount;

}

publicIntegergetNewAdminCount(){

returnnewAdminCount;

}

publicvoidsetNewAdminCount(IntegernewAdminCount){

this.newAdminCount=newAdminCount;

}

publicIntegergetOldActiveCount(){

returnoldActiveCount;

}

publicvoidsetOldActiveCount(IntegeroldActiveCount){

this.oldActiveCount=oldActiveCount;

}

publicIntegergetOldAdminCount(){

returnoldAdminCount;

}

publicvoidsetOldAdminCount(IntegeroldAdminCount){

this.oldAdminCount=oldAdminCount;

}

publicStringtoString(){

returnToStringBuilder.reflectionToString(this);

}

}

RepositoryInterface

packagecom.sillycat.jobsmonitorapi.repository;

importjava.util.List;

importorg.springframework.data.cassandra.repository.Query;

importorg.springframework.data.repository.CrudRepository;

importcom.sillycat.jobsmonitorapi.domain.JobCountHistory;

publicinterfaceJobCountHistoryRepositoryCassandraextendsCrudRepository<JobCountHistory,String>{

@Query("select*fromjobcounthistorywheresource_id=?0")

publicList<JobCountHistory>findBySourceID(StringsourceID);

@Query("deletefromjobcounthistorywheresource_id=?0")

publicvoiddeleteBySourceID(StringsourceID);

}

UnitTestClass

package

com.sillycat.jobsmonitorapi.repository;

importjava.util.Date;import

java.util.List;

importorg.apache.http.util.Asserts;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.context.SpringBootTest;import

org.springframework.test.context.junit4.SpringRunner;

import

com.sillycat.jobsmonitorapi.domain.JobCountHistory;

@RunWith(SpringRunner.class)@SpringBootTest

publicclass

JobCountHistoryCassandraTest{

@Autowired

JobCountHistoryRepositoryCassandrajobCountHistoryRepositoryCassandra;

@Test

publicvoidsave()throwsException{

jobCountHistoryRepositoryCassandra.deleteBySourceID("9527");

JobCountHistoryitem1=newJobCountHistory();

item1.setNewActiveCount(1);

item1.setNewAdminCount(12);

item1.setOldActiveCount(12);

item1.setOldAdminCount(1);

item1.setSourceID("9527");

item1.setRecordDate(newDate());

jobCountHistoryRepositoryCassandra.save(item1);

List<JobCountHistory>result=jobCountHistoryRepositoryCassandra.findBySourceID("9527");

Asserts.notNull(result,"resultisnotnull");

}

}

References:

相关推荐