当我使用hectorTemplate来操作Cassandra时,如何在spring MVC中使用不同的键空间?

时间:2013-09-15 04:09:24

标签: spring-mvc cassandra

首先,为什么我要使用不同的键空间? 因为我想编写JUnit Test,但我需要另一个密钥空间来进行测试。 我正在使用Spring MVC。 我使用在春天自动装配的hectorTemplate。

<!--  cassandra configuration -->
    <bean id="cassandraHostConfigurator" class="me.prettyprint.cassandra.service.CassandraHostConfigurator">
        <constructor-arg value="${cassandra.url}" />
    </bean>

    <bean id="cluster" class="me.prettyprint.cassandra.service.ThriftCluster">
        <constructor-arg value="${cassandra.cluster}" />
        <constructor-arg ref="cassandraHostConfigurator" />
    </bean>

    <bean id="consistencyLevelPolicy" class="me.prettyprint.cassandra.model.ConfigurableConsistencyLevel">
       <property name="defaultReadConsistencyLevel" value="${cassandra.defaultReadConsistencyLevel}"></property>
       <property name="defaultWriteConsistencyLevel" value="${cassandra.defaultWriteConsistencyLevel}"></property>
    </bean>

    <bean id="keyspace" class="me.prettyprint.hector.api.factory.HFactory" factory-method="createKeyspace">
        <constructor-arg value="${cassandra.keyspace}" />
        <constructor-arg ref="cluster" />
        <constructor-arg ref="consistencyLevelPolicy" />
    </bean>

    <bean id="hectorTemplate" class="me.prettyprint.cassandra.service.spring.HectorTemplateImpl">
        <property name="cluster" ref="cluster" />
        <property name="keyspace" ref="keyspace" />
        <property name="replicationStrategyClass" value="org.apache.cassandra.locator.SimpleStrategy" />
        <property name="replicationFactor" value="1" />
    </bean>

所以我应该做什么来添加另一个键空间? 谢谢你的帮助。(n.n)!!!!!

1 个答案:

答案 0 :(得分:0)

<!-- Keyspace1 -->

 <bean id="keyspace1" class="me.prettyprint.hector.api.factory.HFactory" factory-method="createKeyspace">
        <constructor-arg value="${cassandra.keyspace1}" />
        <constructor-arg ref="cluster" />
        <constructor-arg ref="consistencyLevelPolicy" />
    </bean>

    <bean id="hectorTemplate1" class="me.prettyprint.cassandra.service.spring.HectorTemplateImpl">
        <property name="cluster" ref="cluster" />
        <property name="keyspace" ref="keyspace1" />
        <property name="replicationStrategyClass" value="org.apache.cassandra.locator.SimpleStrategy" />
        <property name="replicationFactor" value="1" />
    </bean>


<!-- Keyspace2 -->

 <bean id="keyspace2" class="me.prettyprint.hector.api.factory.HFactory" factory-method="createKeyspace">
        <constructor-arg value="${cassandra.keyspace2}" />
        <constructor-arg ref="cluster" />
        <constructor-arg ref="consistencyLevelPolicy" />
    </bean>

    <bean id="hectorTemplate2" class="me.prettyprint.cassandra.service.spring.HectorTemplateImpl">
        <property name="cluster" ref="cluster" />
        <property name="keyspace" ref="keyspace2" />
        <property name="replicationStrategyClass" value="org.apache.cassandra.locator.SimpleStrategy" />
        <property name="replicationFactor" value="1" />
    </bean>

对于JUnit测试,您应该使用相同的键空间并使用嵌入式 Cassandra。检查 here 。没有理由为测试创建另一个密钥空间。