Springboot3.3 整合Cassandra 4.1.5的詳細(xì)過程
一、數(shù)據(jù)庫搭建
-- 創(chuàng)建Keyspace
CREATE KEYSPACE school WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1};
-- 創(chuàng)建表
CREATE TABLE student(
id int PRIMARY KEY,
name text,
age int,
genders int,
address text ,
interest set<text>,
phone list<text>,
education map<text, text>
);二、引入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-cassandra</artifactId> </dependency> <!-- hutool是下面多線程導(dǎo)入數(shù)據(jù)引入 --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.26</version> </dependency>
三、配置文件
spring:
application:
name: Spring-Cassandra
cassandra:
keyspace-name: school
contact-points:
- 192.168.204.131:9042
port: 9042
username: ***
password: ******
local-datacenter: datacenter1
request:
timeout: 60s檢查Cassandra的local-datacenter,可執(zhí)行下面命令:
[root@localhost apache-cassandra]# bin/nodetool status

四、創(chuàng)建一個(gè)實(shí)體類:
import lombok.Data;
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Data
@Table(value="student")
public class Student implements Serializable {
@PrimaryKey
private Integer id;
@Column("name")
private String name;
@Column("age")
private Integer age;
@Column("genders")
private Integer genders;
@Column("address")
private String address;
@Column("interest")
private Set<String> interest;
@Column("phone")
private List<String> phone;
@Column("education")
private Map<String, String> education;
}五、創(chuàng)建一個(gè)Controller
private final StudentService studentService;
@GetMapping("student")
public List<Student> getStudentByName(String name) {
return studentService.getStudentByName(name);
}
@GetMapping("count")
public Long count() {
return studentService.count();
}
@GetMapping("list")
public List<Student> list(String name) {
Student student = new Student();
student.setName(name);
return studentService.list(student);
}
@GetMapping("save")
public String sava() {
for(int i = 0; i<100; i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
int finalI = i;
ThreadUtil.execute(() -> {
System.out.println("線程"+ finalI +"運(yùn)行");
List<Student> list = Lists.newArrayList();
for(int j=0; j<100; j++) {
UUID uuid = UUID.randomUUID();
int hash = uuid.toString().hashCode();
int maxTenDigit = (int) Math.pow(10, 10) - 1; // 10位整數(shù)的最大值
int modHash = Math.abs(hash % maxTenDigit);
Student student = new Student();
student.setId(modHash);
int random = (int) (Math.random() * 100);
student.setName("GG"+ random);
student.setAge(random);
student.setGenders(1);
student.setAddress("China");
list.add(student);
// studentService.save(student);
}
System.out.println("線程"+ finalI +"開始批量插入");
studentService.batchSave(list);
System.out.println("線程"+ finalI +"結(jié)束批量插入");
});
}
System.out.println("線程結(jié)束");
// ThreadUtil.waitForDie();
return "success";
}注意:Cassandra 本身不適合用來做數(shù)據(jù)分析統(tǒng)計(jì),比如 count,是需要去遍歷數(shù)據(jù)庫的,分布式數(shù)據(jù)庫,那么就要通通遍歷一次。小數(shù)據(jù)還可以,數(shù)據(jù)量大會(huì)報(bào)查詢超時(shí)錯(cuò)誤。
六、編寫service查詢數(shù)據(jù)
service接口省略
1、JPA方式查詢
service實(shí)現(xiàn)
private final StudentMapper studentMapper;
@Override
public List<Student> getStudentByName(String name) {
return studentMapper.getStudentByName(name);
}mapper接口
@Query(value = "select id, address, age, genders, name, interest, phone, education from student where name = ?0 ALLOW filtering") List<Student> getStudentByName(String name);
Allow filtering:
如果你的查詢條件里,有一個(gè)是根據(jù)索引查詢,那其它非索引非主鍵字段,可以通過加一個(gè)ALLOW FILTERING來過濾實(shí)現(xiàn);
雖然查詢非索引非主鍵字段,但是只要加了ALLOW FILTERING條件,它會(huì)先根據(jù)索引查出來的值,再對結(jié)果進(jìn)行過濾;
(如果不加ALLOW FILTERING,而又有非索引列,這樣是不允許的; 加上ALLOW FILTERING,相當(dāng)于在結(jié)果后再進(jìn)行過濾。)

2、CassandraTemplate方式查詢
private final CassandraTemplate cassandraTemplate;
@Override
public List<Student> list(Student queryInfo) {
Query query = Query
.query(where("name").is(queryInfo.getName()));
query = query.withAllowFiltering();
query = query.columns(Columns.from("id", "name", "age", "address", "genders", "interest", "phone", "education"));
System.out.println(query);
return this.cassandraTemplate.select(query, Student.class);
}
@Override
public void save(Student student) {
this.cassandraTemplate.insert(student);
}
/**
* 批量插入
*/
@Override
public void batchSave(List<Student> list) {
CassandraBatchOperations batchOps = cassandraTemplate.batchOps();
batchOps.insert(list);
batchOps.execute();
}
@Override
public Long count() {
return cassandraTemplate.count(Student.class);
}到此這篇關(guān)于Springboot3.3 整合Cassandra 4.1.5的文章就介紹到這了,更多相關(guān)Springboot整合Cassandra內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中volatile關(guān)鍵字的作用與實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于java中volatile關(guān)鍵字的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Java前端Layer.open.btn驗(yàn)證無效解決方法
在本篇文章里我們給大家整理了一篇關(guān)于Java前端Layer.open.btn驗(yàn)證無效解決方法以及實(shí)例代碼,需要的朋友們可以參考學(xué)習(xí)下。2019-09-09
Java中使用MongoDB數(shù)據(jù)庫實(shí)例Demo
MongoDB是由C++語言編寫的,基于分布式文件存儲(chǔ)的數(shù)據(jù)庫,是一個(gè)介于關(guān)系數(shù)據(jù)庫和非關(guān)系數(shù)據(jù)庫之間的產(chǎn)品,是最接近于關(guān)系型數(shù)據(jù)庫的NoSQL數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Java中使用MongoDB數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下2023-12-12
mybatisplus如何在xml的連表查詢中使用queryWrapper
這篇文章主要介紹了mybatisplus如何在xml的連表查詢中使用queryWrapper,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
java中Hashtable和HashMap的區(qū)別分析
java中Hashtable和HashMap的區(qū)別分析,需要的朋友可以參考一下2013-04-04

