spring data with cassandra

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的一个具体实现。

你可能感兴趣的:(spring data with cassandra)