1.工具spring-data-cassandra.
2.增改:
@Autowired
CassandraOperations operations;
public void insertUserPrivilege(UserPrivilegeCassandra userprivilege) {
Insert insert = QueryBuilder.insertInto("userprivilege");
insert.setConsistencyLevel(ConsistencyLevel.QUORUM);
insert.value("passport",userprivilege.getPassport());
insert.value("privilegeId",userprivilege.getPrivilegeId());
insert.value("updateTime",userprivilege.getUpdateTime());
insert.value("privileges", userprivilege.getPrivileges());
operations.execute(insert);
}
3.查:
<span style="white-space:pre"> </span>public UserPrivilegeCassandra selectUserPrivilege(String passport,Long privilgeId) {
Select select = QueryBuilder.select().from("userprivilege");
select.setConsistencyLevel(ConsistencyLevel.QUORUM);
select.where(QueryBuilder.eq("passport", passport));
select.where(QueryBuilder.eq("privilegeId", privilgeId));
UserPrivilegeCassandra userprivilege = operations.selectOne(select,
UserPrivilegeCassandra.class);
return userprivilege;
}
4.配置:
pom:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
<version>1.3.0.RELEASE</version>
</dependency>
xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:cassandra="http://www.springframework.org/schema/data/cassandra"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd">
<context:component-scan base-package="com.sohu.passport.dao.*">
</context:component-scan>
<cassandra:cluster contact-points="${cassandra.contactpoints}"
port="${cassandra.port}" />
<cassandra:session keyspace-name="${cassandra.keyspace}" />
<cassandra:mapping />
<cassandra:converter />
<cassandra:template id="cqlTemplate" />
<cassandra:repositories base-package="com.sohu.dao.cassandra" />
</beans>
5.自己整了個util,沒有通過spring-data-redis處理:
package com.sohu.util;
import java.util.ResourceBundle;
import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.SimpleStatement;
import com.datastax.driver.core.Statement;
public class CassandraTools {
private static CassandraTools instance;
private CassandraTools() {
}
public static synchronized CassandraTools getInstance() {
if (instance == null) {
instance = new CassandraTools();
instance.init();
}
return instance;
}
Cluster cluster;
Session session;
public void init() {
if (cluster == null) {
cluster = new Cluster.Builder().addContactPoint(ResourceBundle.getBundle("cassandra").getString("cassandra.contactpoints").split(",")[0])
.build();
if (session == null) {
session = cluster.connect("uc_passport");
}
}
}
/**
* 這個得多看看源碼才行。。
* @param cql
* @param cl
* @return
*/
public ResultSet execute(String cql,ConsistencyLevel cl) {
Statement st=new SimpleStatement(cql);
st.setConsistencyLevel(cl);
st.setFetchSize(1000);
System.out.println(st.toString());
ResultSet rs = session.execute(st);
// rs.forEach(n -> {
// System.out.println(n);
// });
return rs;
}
public static void main(String[] args) {
String cql = "SELECT * FROM usertoken";
ResultSet rs = CassandraTools.getInstance().execute(cql,ConsistencyLevel.QUORUM);
rs.forEach(n -> {
System.out.println(n);
});
}
}
6.備注:
cql文檔:http://docs.datastax.com/en/cql/3.1/cql/cql_using/use_map_t.html