1、depedency
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
2、properties
spring.data.cassandra.keyspace-name=XXXX
spring.data.cassandra.contact-points=XXXX
spring.data.cassandra.port=1111
spring.data.cassandra.username=xxxxx
spring.data.cassandra.password=xxxxx
spring.data.cassandra.session-name=xxxxxx
3、config
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
import org.springframework.data.cassandra.config.CqlSessionFactoryBean;
@Configuration
public class CassandraConfig extends AbstractCassandraConfiguration {
@Value("${spring.data.cassandra.keyspace-name}")
private String keyspaceName;
@Value("${spring.data.cassandra.contact-points}")
private String contactPoints;
@Value("${spring.data.cassandra.username}")
private String username;
@Value("${spring.data.cassandra.password}")
private String password;
@Value("${spring.data.cassandra.session-name}")
private String sessionName;
@Value("${spring.data.cassandra.port}")
private int port;
@Override
public String getKeyspaceName() {
return keyspaceName;
}
@Override
public String getContactPoints() {
return contactPoints;
}
@Override
public String getSessionName() {
return sessionName;
}
@Override
public String getLocalDataCenter() {
return "dc-qcsh";
}
@Override
public CqlSessionFactoryBean cassandraSession() {
CqlSessionFactoryBean cqlSessionFactoryBean = super.cassandraSession();
cqlSessionFactoryBean.setPassword(password);
cqlSessionFactoryBean.setUsername(username);
cqlSessionFactoryBean.setPort(port);
cqlSessionFactoryBean.setKeyspaceName(keyspaceName);
cqlSessionFactoryBean.setContactPoints(contactPoints);
return cqlSessionFactoryBean;
}
}
4、使用
@Resource
private CassandraTemplate cassandraTemplate;
// 批量插入
public void insert(){
CassandraBatchOperations batchOps = cassandraTemplate.batchOps();
batchOps.insert(List<entity>);
batchOps.execute();
}
标签:String,spring,boot,private,cqlSessionFactoryBean,Cassandra,data,cassandra
From: https://www.cnblogs.com/qingxuan0316/p/17162076.html