加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – spring-data-cassandra存储库的多个密钥空间支持?

发布时间:2020-12-15 00:43:02 所属栏目:Java 来源:网络整理
导读:Spring Data Cassandra是否支持同一应用程序上下文中的多个键空间存储库?我正在使用以下 JavaConfig类设置cassandra spring数据配置 @Configuration@EnableCassandraRepositories(basePackages = "com.blah.repository")public class CassandraConfig exten
Spring Data Cassandra是否支持同一应用程序上下文中的多个键空间存储库?我正在使用以下 JavaConfig类设置cassandra spring数据配置
@Configuration
@EnableCassandraRepositories(basePackages = "com.blah.repository")
public class CassandraConfig extends AbstractCassandraConfiguration {

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

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

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

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

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

spring-data-cassandra是否支持多个密钥空间存储库?我找到多个键空间的引用的唯一地方是here.但它没有解释是否可以使用存储库完成此操作?

解决方法

工作APP样本:
http://valchkou.com/spring-boot-cassandra.html#multikeyspace

您需要的Idea会覆盖默认bean:s??essionfactory和template

样品:

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的实现

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,只需使用不同的包
package com.sample.repo.test2;

5)在专用包中为每个键空间放置repo

package com.sample.repo.test1;

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


package com.sample.repo.test2;

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读