使用Spring Data Cassandra对多个名称空间运行查询 [英] Run Query against multiple namespaces with Spring Data Cassandra

查看:136
本文介绍了使用Spring Data Cassandra对多个名称空间运行查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用Spring Data在Cassandra中的所有键空间上执行查询?

Is there any way in which using Spring Data a query can be executed on all keyspaces in Cassandra?

推荐答案

有答案的两个部分:


  1. 使用Spring Data Cassandra 1.x时,您需要设置单个 CassandraTemplate 您要使用的每个键空间的实例。

  2. 借助Spring Data Cassandra 2.x,我们引入了 SessionFactory 接口来控制要使用哪个 Session 。我们随附路由 SessionFactory 支持,因此您可以提供多个会话和一个鉴别符(通常(基于 ThreadLocal ))来选择适当的 Session

  1. When using Spring Data Cassandra 1.x, you are need to setup individual CassandraTemplate instances for each keyspace you want to use.
  2. With Spring Data Cassandra 2.x, we introduced the SessionFactory interface to control which Session to use. We ship with routing SessionFactory support so you can provide multiple sessions and a discriminator (usually something ThreadLocal-based) to select the appropriate Session.

某些示例代码如下:

class MyRoutingSessionFactory extends AbstractRoutingSessionFactory {

    ThreadLocal<String> lookupKey = ThreadLocal.withInitial(() -> "default-session");

    void setLookupKey(String lookupKey) {
        this.lookupKey.set(lookupKey);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return lookupKey.get();
    }
}

class MyConfig extends AbstractCassandraConfiguration {

    @Bean
    @Override
    public SessionFactory sessionFactory() {

        MyRoutingSessionFactory factory = new MyRoutingSessionFactory();
        factory.setDefaultTargetSessionFactory(getRequiredSession());

        MapSessionFactoryLookup lookup = new MapSessionFactoryLookup();

        Session myOtherSession = …;

        lookup.addSessionFactory("default-session", getRequiredSession());          
        lookup.addSessionFactory("my-other-session", myOtherSession);

        factory.setSessionFactoryLookup(lookup);

        return factory;
    }

    // …
}

这篇关于使用Spring Data Cassandra对多个名称空间运行查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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