spring data with cassandra
2015-06-23 00:00
316 查看
摘要: spring data with cassandra 使用参考。不足地方,请各位多多指出。
spring 连接cassandra 基础配置
property文件
cluster.contactPoints=10.0.1.4
cluster.port=9042
cluster.compression=SNAPPY
cluster.metricsEnabled=false
cluster.jmxReportingEnabled=false
cluster.reconnection.delayMillis=5000
cluster.sslEnabled= true
keyspace.name=test_baby
keyspace.action=ALTER
dc1.name=DCJAX
dc1.rf=2
dc2.name=DCCTL
dc2.rf=3
lb.policy.dcAware.remoteHosts=5
lb.policy.dcAware.localDc=DCJAX
auth.username=lizhen
auth.password=lizhen123
socket.connectTimeoutMillis=5000
socket.keepAlive=true
socket.readTimeoutMillis=60000
socket.receiveBufferSize=1024
socket.sendBufferSize=2048
socket.reuseAddress=true
socket.soLinger=5
socket.tcpNoDelay=false
local.min.requests=10
local.max.requests=20
local.core.connections=4
local.max.connections=8
remote.min.requests=5
remote.max.requests=10
remote.core.connections=2
remote.max.connections=4
1.持久化层使用 spring data (提供一套数据访问层的解决方案,减少了数据访问层的开发量)技术 ,只需要声明自己的持久化接口继承现有的respotiey(例如,CrudRepository ,PagingAndSortingRepository),spring 通过扫描这些接口,生成与目标数据库操作的具体实现
2.由于cassandra 不支持事务,对于多个表的操作可以放入到一个batch里面处理,能过取到事务的目的(cassandra2.0+)
其中 CassandraTemplate 是 CassandraOperation的一个具体实现。
spring 连接cassandra 基础配置
<?xml version='1.0'?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cassandra="http://www.springframework.org/schema/data/cassandra" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/cql http://www.springframework.org/schema/data/cassandra/spring-cql-1.0.xsd http://www.springframework.org/schema/data/cassandra http://www.springframework.org/schema/data/cassandra/spring-cassandra-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- <context:component-scan base-package="com.yxtar.server.cassandra.dao"></context:component-scan> --> <context:component-scan base-package="com.yxtar.server.service"></context:component-scan> <context:component-scan base-package="com.yxtar.server.serviceImpl"></context:component-scan> <context:component-scan base-package="com.yxtar.server.servlet"></context:component-scan> <context:property-placeholder location="classpath:cassandra.properties" /> <!-- <bean id="hostStateListener" class="org.springframework.cassandra.test.integration.config.xml.TestHostStateListener" /> <bean id="latencyTracker" class="org.springframework.cassandra.test.integration.config.xml.TestLatencyTracker" /> --> <!-- 权限 策略 --> <bean id="authProvider" class="com.datastax.driver.auth.DseAuthProvider" /> <!-- 负载均衡 策略 --> <bean id="loadBalancingPolicy" class="com.datastax.driver.core.policies.DCAwareRoundRobinPolicy"> <constructor-arg name="localDc" value="${lb.policy.dcAware.localDc}" /> <constructor-arg name="usedHostsPerRemoteDc" value="${lb.policy.dcAware.remoteHosts}" /> </bean> <!-- --> <bean id="reconnectionPolicy" class="com.datastax.driver.core.policies.ConstantReconnectionPolicy"> <constructor-arg name="constantDelayMs" value="${cluster.reconnection.delayMillis}" /> </bean> <!-- --> <bean id="retryPolicy" class="com.datastax.driver.core.policies.DowngradingConsistencyRetryPolicy" /> <!-- orm --> <cassandra:mapping entity-base-packages="com.flamingo.positive.dto"> <!-- <cassandra:entity class="com.flamingo.positive.dto.News"> <cassandra:table name="news" /> </cassandra:entity> --> </cassandra:mapping> <!-- 类型转换 --> <cassandra:converter /> <!-- --> <cassandra:cluster contact-points="${cluster.contactPoints}" port="${cluster.port}" compression="${cluster.compression}" auth-info-provider-ref="authProvider" username="${auth.username}" password="${auth.password}" metrics-enabled="${cluster.metricsEnabled}" jmx-reporting-enabled="${cluster.jmxReportingEnabled}" reconnection-policy-ref="reconnectionPolicy" retry-policy-ref="retryPolicy" load-balancing-policy-ref="loadBalancingPolicy"> <cassandra:local-pooling-options min-simultaneous-requests="${local.min.requests}" max-simultaneous-requests="${local.max.requests}" core-connections="${local.core.connections}" max-connections="${local.max.connections}" /> <cassandra:remote-pooling-options min-simultaneous-requests="${remote.min.requests}" max-simultaneous-requests="${remote.max.requests}" core-connections="${remote.core.connections}" max-connections="${remote.max.connections}" /> <cassandra:socket-options connect-timeout-millis="${socket.connectTimeoutMillis}" keep-alive="${socket.keepAlive}" read-timeout-millis="${socket.readTimeoutMillis}" reuse-address="${socket.reuseAddress}" so-linger="${socket.soLinger}" tcp-no-delay="${socket.tcpNoDelay}" receive-buffer-size="${socket.receiveBufferSize}" send-buffer-size="${socket.sendBufferSize}" /> <cassandra:keyspace name="${keyspace.name}" action="${keyspace.action}" /> </cassandra:cluster> <!-- 当前使用scheam --> <cassandra:session keyspace-name="${keyspace.name}" /> <!-- <bean id="cqlTemplate" class="org.springframework.cassandra.core.CqlTemplate"> <constructor-arg ref="cassandraSession" /> </bean> --> <!-- cassandra operater --> <cassandra:template id="cqlTemplate" /> <!-- spring data 接口 --> <cassandra:repositories base-package="com.yxtar.server.cassandra.dao" /> </beans>
property文件
cluster.contactPoints=10.0.1.4
cluster.port=9042
cluster.compression=SNAPPY
cluster.metricsEnabled=false
cluster.jmxReportingEnabled=false
cluster.reconnection.delayMillis=5000
cluster.sslEnabled= true
keyspace.name=test_baby
keyspace.action=ALTER
dc1.name=DCJAX
dc1.rf=2
dc2.name=DCCTL
dc2.rf=3
lb.policy.dcAware.remoteHosts=5
lb.policy.dcAware.localDc=DCJAX
auth.username=lizhen
auth.password=lizhen123
socket.connectTimeoutMillis=5000
socket.keepAlive=true
socket.readTimeoutMillis=60000
socket.receiveBufferSize=1024
socket.sendBufferSize=2048
socket.reuseAddress=true
socket.soLinger=5
socket.tcpNoDelay=false
local.min.requests=10
local.max.requests=20
local.core.connections=4
local.max.connections=8
remote.min.requests=5
remote.max.requests=10
remote.core.connections=2
remote.max.connections=4
1.持久化层使用 spring data (提供一套数据访问层的解决方案,减少了数据访问层的开发量)技术 ,只需要声明自己的持久化接口继承现有的respotiey(例如,CrudRepository ,PagingAndSortingRepository),spring 通过扫描这些接口,生成与目标数据库操作的具体实现
@Repository public interface UserRespository extends CrudRepository<Users, UUID> { @Query("select * from user_email where email=?0") public Users findUsersByEmail(String email); }
2.由于cassandra 不支持事务,对于多个表的操作可以放入到一个batch里面处理,能过取到事务的目的(cassandra2.0+)
public class BatchUtility { @Autowired private CassandraOperations operation; /** * 进行批量处理 */ public void batch(List<SimpleStatement> statements) { BatchStatement batch = new BatchStatement(); for (SimpleStatement statement : statements) { batch.add(statement); } operation.execute(batch); }
其中 CassandraTemplate 是 CassandraOperation的一个具体实现。
相关文章推荐
- java 读取属性文件
- Java多线程编程总结
- java获取指定地址图片高度宽度简单代码
- Java基础——进制转换
- Java八大基本数据类型-详解
- JAVA 中 Map的遍历
- Java中urlrewriter的使用
- myeclipse9 maven web 环境
- Java基础:异常捕获顺序
- Java基础:抽象类和接口
- Java基础—集合框架
- 11.MyBatis与Spring整合
- Java基础:多态(重载和重写)
- JAVA基础—IO体系分析
- 利用TreeSet,按照姓名长度的大小决定存储的顺序,从长到短排序,如果长度一样,年龄小的在前面,源码
- JDK自带的JVM性能调优监控工具简介
- Spring学习总结(2.3)-Spring MVC:handlerAdapter
- Java基础:初始化和清理
- java设计模式之前
- Spring2.5中的JavaMailSenderImpl、MimeMessageHelper