1.声明
当前内容主要为使用SpringBoot-data方式操作当前的Linux下的Cassandra的基本查询添加操作(主要用于本人学习和复习之用),其主要内容如下:
- 使用cqlsh方式创建keysapce
- 使用keyspace
- 创建table和查询table操作
- 使用SpringBoot-data操作Cassandra数据库
2.基本demo
1.pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.18.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
2.最重要的配置类:CassandraConfig.java
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Configuration
@EnableCassandraRepositories(basePackages = "com.hy.springboot.cassandra.dao")
@EntityScan(basePackages = "com.hy.springboot.cassandra.entity")
@EnableTransactionManagement
public class CassandraConfig extends AbstractCassandraConfiguration {
@Override
public String getContactPoints() {
return "192.168.1.100";
}
@Override
protected String getKeyspaceName() {
return "springdata";
}
}
这里借鉴官方github的使用
3.基本的实体类:Person.java
import org.springframework.data.cassandra.core.mapping.Column;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
@Table
public class Person {
@PrimaryKey
private int id;
@Column
private String name;
@Column
private int age;
// 省略get、set、toString方法
}
4.基本的dao:PersonRepository.java
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.hy.springboot.cassandra.entity.Person;
@Repository
public interface PersonRepository extends CrudRepository<Person, Integer> {
}
5.基本的controller层:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hy.springboot.cassandra.dao.PersonRepository;
import com.hy.springboot.cassandra.entity.Person;
@RestController
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping("/findAllPerson")
public Object findAllPerson() {
return personRepository.findAll();
}
@RequestMapping("/savePerson")
public Object savePerson(Person person) {
personRepository.save(person);
return "success";
}
}
6.基本的入口类:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author admin
* @createTime 2021-02-28 09:07:13
* @description 当前内容主要用于使用SpringData方式操作当前的Cassandra4.0版本
*
*/
@SpringBootApplication
public class SpringBootCassandraApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootCassandraApplication.class, args);
}
}
3.启动中出现的问题
问题1:Caused by: com.datastax.driver.core.exceptions.SyntaxError: Error executing "USE springdata" (line 1:4 no viable alternative at input 'springdata' (USE [springdata])). Check that your keyspace name is valid
问题原因:缺少springdata这个keyspace,在执行use springdata时候出现错误!
解决办法:手动使用cqlsh方式创建springdata这个keyspace
CREATE KEYSPACE springdata WITH replication = {
'class': 'NetworkTopologyStrategy', 'replication_factor' : 3}
结果报错:
ConfigurationException: replication_factor is an option for SimpleStrategy, not NetworkTopologyStrategy
,原因就是只能在SimpleStrategy中使用replication_factor ,而NetworkTopologyStrategy中不允许
解决为:CREATE KEYSPACE springdata WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3}
查看所有的keyspace:desc keyspaces;
2.访问时报错:localhost:8080/findAllPerson
com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table person 这个错误,是没有表导致的
默认会执行cql:select * from person,本人以为springdata会自动创建数据表,结果没有!
解决办法:手动创建Person表
use springdata;
CREATE TABLE person (
id int PRIMARY KEY,
name text,
age int
);
再次访问:
4.执行的添加查询操作
1.添加数据操作:
查询操作:
执行成功!
5.总结
1.本人一贯受到SpringData的影响,以为SpringData会自动创建keysapce和table
,所以导致在实际操作中出现了许多问题,但这些问题都可以解决
2.个人感觉这个Cassandra这个数据库的操作语法和MySql的很像,但是区别在于一个是database,一个是keyspace
,其他的还好!