在学习完keyspace的创建后,后续的练习都可以同步在命令行和JAVA API中执行。因此,在这里暂停一下,我们建一个项目用于练习操作我们本机搭的这个Cassandra。
我目前手头的环境只有一个IntelliJ和一个JDK8,所以需要补充一些内容。
1. 安装Spring Boot项目创建的便捷插件:
https://blog.csdn.net/chy555chy/article/details/84970042
2. 创建一个Spring Boot项目,需要勾选上对Cassandra相关功能的依赖。
我勾了Cassandra、web、test:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--需要手动补充-->
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.8.0</version>
</dependency>
3. 创建好项目后,修改配置文件application.yml:
spring:
data:
cassandra:
keyspace-name: tutorialspoint
#entity-base-packages:
contact-points: 127.0.0.1
port: 9042
cluster-name: Test Cluster
4. 建一些测试数据,也是别处参考,忽略起名不规范的问题:
CREATE TABLE tutorialspoint.users (
userid int PRIMARY KEY,
fname text,
lname text
);
insert into users (userid, fname, lname ) VALUES ( 1,'john','smith');
insert into users (userid, fname, lname ) VALUES ( 2,'john','tiger');
create index on users(lname);
注: 需要在查询条件中用到的字段建index
5. 写代码用CassandraTemplate进行常规操作:
实体类(映射到表):
package com.shinyke.demo.cassandra.entity;
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
import org.springframework.data.cassandra.core.mapping.Table;
@Table("users") //Cassandra的表注解,value指定表名
public class User{
@PrimaryKey
private int userid;
private String fname;
private String lname;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}
Dao:
package com.shinyke.demo.cassandra.dao;
import com.shinyke.demo.cassandra.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class UserDao{
@Autowired
private CassandraTemplate cassandraTemplate;
public List<User> getAll() {
List<User> ret = cassandraTemplate.select("SELECT userid, fname, lname FROM users", User.class);
// Gson gson = new Gson();
// System.out.println("ret <"+ gson.toJson(ret) +">");
return ret;
}
}
写个Controller用于调用:
package com.shinyke.demo.cassandra.controller;
import com.google.gson.Gson;
import com.shinyke.demo.cassandra.dao.UserDao;
import com.shinyke.demo.cassandra.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
@RequestMapping("/list")
public List<User> list() {
List<User> ret = userDao.getAll();
Gson gson = new Gson();
System.out.println("ret <"+ gson.toJson(ret) +">");
return ret;
}
}
直接用所有默认设置运行起来:
执行结果:
Done