Cassandra操作备份

来源:互联网 发布:传奇怪物补丁算法 编辑:程序博客网 时间:2024/06/05 08:23

1.工具spring-data-cassandra.

2.增改:

@AutowiredCassandraOperations 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.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsdhttp://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




0 0
原创粉丝点击