对spring-data-cassandra存储库有多个键空间支持? [英] Multiple keyspace support for spring-data-cassandra repositories?

查看:130
本文介绍了对spring-data-cassandra存储库有多个键空间支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Data Cassandra是否在同一应用程序上下文中支持多个键空间存储库?我正在使用以下JavaConfig类设置cassandra spring数据配置

Does Spring Data Cassandra support multiple keyspace repositories in the same application context? I am setting up the cassandra spring data configuration using the following JavaConfig class

@Configuration
@EnableCassandraRepositories(basePackages = "com.blah.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {

@Override
public String getKeyspaceName() {
    return "keyspace1";
}

在将存储库类移动到其他软件包之后,我尝试创建第二个配置类.

I tried creating a second configuration class after moving the repository classes to a different package.

@Configuration
@EnableCassandraRepositories(basePackages = "com.blah.secondrepository")
public class SecondCassandraConfig extends AbstractCassandraConfiguration {

@Override
public String getKeyspaceName() {
    return "keyspace2";
}

但是,在这种情况下,如果存储库失败(因为在键空间中找不到为实体配置的列族),则第一组.我认为它可能正在第二个键空间中寻找列族.

However in that case the first set if repositories fail as the configured column family for the entities is not found in the keyspace. I think it is probably looking for the column family in the second keyspace.

spring-data-cassandra是否支持多个键空间存储库?我找到多个键空间引用的唯一地方是

Does spring-data-cassandra support multiple keyspace repositories? The only place where I found a reference for multiple keyspaces was here. But it does not explain if this can be done with repositories?

推荐答案

正在运行的APP示例: http://valchkou.com/spring-boot-cassandra.html#multikeyspace

Working APP Sample: http://valchkou.com/spring-boot-cassandra.html#multikeyspace

您需要的想法将覆盖默认bean:s​​essionfactory和模板

The Idea you need override default beans: sessionfactory and template

示例:

1)application.yml

1) application.yml

 spring:
  data:
    cassandra:
      test1:
        keyspace-name: test1_keyspace
        contact-points: localhost
      test2:
        keyspace-name: test2_keyspace
        contact-points: localhost

2)基本配置类

public abstract class CassandraBaseConfig extends AbstractCassandraConfiguration{
    protected String contactPoints;
    protected String keyspaceName;

    public String getContactPoints() {
        return contactPoints;
    }
    public void setContactPoints(String contactPoints) {
        this.contactPoints = contactPoints;
    }

    public void setKeyspaceName(String keyspaceName) {
        this.keyspaceName = keyspaceName;
    }
    @Override
    protected String getKeyspaceName() {
        return keyspaceName;
    }
}

3)test1的配置实现

3) Config implementation for test1

package com.sample.repo.test1;

@Configuration
@ConfigurationProperties("spring.data.cassandra.test1")
@EnableCassandraRepositories(
        basePackages = "com.sample.repo.test1",
        cassandraTemplateRef = "test1Template"
)
public class Test1Config extends CassandraBaseConfig {

    @Override
    @Primary
    @Bean(name = "test1Template")
    public CassandraAdminOperations cassandraTemplate() throws Exception {
        return new CassandraAdminTemplate(session().getObject(), cassandraConverter());
    }

    @Override
    @Bean(name = "test1Session")
    public CassandraSessionFactoryBean session() throws Exception {

        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();

        session.setCluster(cluster().getObject());
        session.setConverter(cassandraConverter());
        session.setKeyspaceName(getKeyspaceName());
        session.setSchemaAction(getSchemaAction());
        session.setStartupScripts(getStartupScripts());
        session.setShutdownScripts(getShutdownScripts());

        return session;
    }
}

4)对于test2相同,只是使用不同的包 包com.sample.repo.test2;

4) same for test2, just use different package package com.sample.repo.test2;

5)将每个密钥空间存放在专用包中 即

5) place repo for each keyspace in dedicated package i.e.

package com.sample.repo.test1;

@Repository
public interface RepositoryForTest1 extends CassandraRepository<MyEntity> {
// ....
}


package com.sample.repo.test2;

@Repository
public interface RepositoryForTest2 extends CassandraRepository<MyEntity> {
// ....
}

这篇关于对spring-data-cassandra存储库有多个键空间支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆