【问题标题】:how to use different keyspaces in spring MVC when I use hectorTemplate to operate Cassandra?使用 hectorTemplate 操作 Cassandra 时如何在 Spring MVC 中使用不同的键空间?
【发布时间】:2013-09-19 11:18:12
【问题描述】:

首先,我为什么要使用不同的键空间? 因为我想写 JUnit Test,但是我需要另一个 keyspace 来测试。 我正在使用 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)!!!!!

【问题讨论】:

    标签: spring-mvc cassandra


    【解决方案1】:
    <!-- 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。没有理由仅仅为了测试而创建另一个键空间。

    【讨论】:

    • 感谢您的回复。
    • 但是我不知道如何区分java代码中的两个keyspace,有什么判断吗?因为我需要在服务层测试方法,DAO方法不能修改。跨度>
    • 在 DAO 的测试代码中,比如 MyDaoTest,您应该注入一个由 Cassandra 单元引导的 Hector 集群和键空间。
    猜你喜欢
    • 1970-01-01
    • 2018-04-16
    • 2016-05-10
    • 2017-05-19
    • 2018-04-27
    • 2021-03-28
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    相关资源
    最近更新 更多