Spring Boot and RESTful API(4)Cassandra Spring Data

本文介绍如何使用SpringBoot和Spring Data Cassandra操作Cassandra数据库,包括配置依赖、创建数据表及实现基本的CRUD操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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://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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值