cassandra作为存储大数据量的数据库,难免会遇到,这里简单讲一下配置和使用方式,为了方便直接贴出代码。
pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.11.0</version>
</dependency>
yml配置文件
data:
cassandra:
contact-points: 192.XXX.XX.XXX
port: 9042
local-datacenter: datacenter1
session-name: JtTistCluster
username: XXX
password: XXX
keyspace-name: test
然后再需要使用的的java文件中引入CassandraTemplate工具类
@Autowired
private CassandraTemplate cassandraTemplate;
//然后可以使用对应方法
@ResponseBody
@PostMapping(value = "/test" , produces = "application/json; charset=utf-8")
public JSONObject test(@RequestBody Map<String,Object> map) {
JSONObject result = new JSONObject();
try {
String id=map.get("vDeviceID").toString();
String time = String.valueOf(map.get("time"));
Map<String,String> info = (Map<String, String>) map.get("fields");
Clockshistory clockshistory = new Clockshistory();
clockshistory.setInfo(info);
clockshistory.setTime(time);
clockshistory.setid(id);
cassandraTemplate.insert(clockshistory);
}catch (Exception e){
result.put("flag", 0);
result.put("message", "导入失败");
return result;
}
result.put("flag", 1);
result.put("message", "导入成功");
return result;
}
本文介绍了如何在项目中集成Cassandra数据库,包括必要的pom依赖配置、yml配置文件设置,以及通过示例展示了如何使用CassandraTemplate进行数据插入操作。
&spm=1001.2101.3001.5002&articleId=127036663&d=1&t=3&u=e17f6fe99e7d457c9fd751a2cc9f4c0a)
799

被折叠的 条评论
为什么被折叠?



