Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring数据Cassandra对多个名称空间运行查询_Cassandra_Spring Data_Spring Data Cassandra - Fatal编程技术网

使用Spring数据Cassandra对多个名称空间运行查询

使用Spring数据Cassandra对多个名称空间运行查询,cassandra,spring-data,spring-data-cassandra,Cassandra,Spring Data,Spring Data Cassandra,是否有任何方法可以使用Spring数据对Cassandra中的所有键空间执行查询?这个答案有两个部分: 使用Spring数据Cassandra 1.x时,需要为要使用的每个键空间设置单独的CassandraTemplate实例 使用Spring数据Cassandra 2.x,我们引入了接口来控制要使用的会话。我们提供多个会话和一个鉴别器(通常是基于ThreadLocal)来选择合适的会话 2.0的一些示例代码如下所示: class MyRoutingSessionFactory extends

是否有任何方法可以使用Spring数据对Cassandra中的所有键空间执行查询?

这个答案有两个部分:

  • 使用Spring数据Cassandra 1.x时,需要为要使用的每个键空间设置单独的
    CassandraTemplate
    实例
  • 使用Spring数据Cassandra 2.x,我们引入了接口来控制要使用的
    会话。我们提供多个会话和一个鉴别器(通常是基于
    ThreadLocal
    )来选择合适的
    会话
    2.0的一些示例代码如下所示:

    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;
        }
    
        // …
    }
    
    类MyRoutingSessionFactory扩展了AbstractRoutingSessionFactory{ ThreadLocal lookupKey=ThreadLocal.withInitial(()->“默认会话”); void setLookupKey(字符串lookupKey){ this.lookupKey.set(lookupKey); } @凌驾 受保护的对象确定当前查找键(){ 返回lookupKey.get(); } } 类MyConfig扩展了AbstractCassandraConfiguration{ @豆子 @凌驾 public SessionFactory SessionFactory(){ MyRoutingSessionFactory=新的MyRoutingSessionFactory(); setDefaultTargetSessionFactory(getRequiredSession()); MapSessionFactoryLookup=新的MapSessionFactoryLookup(); 会话myOtherSession=…; addSessionFactory(“默认会话”,getRequiredSession()); addSessionFactory(“我的另一个会话”,myOtherSession); setSessionFactoryLookup(查找); 返回工厂; } // … }
    这个答案有两个部分:

  • 使用Spring数据Cassandra 1.x时,需要为要使用的每个键空间设置单独的
    CassandraTemplate
    实例
  • 使用Spring数据Cassandra 2.x,我们引入了接口来控制要使用的
    会话。我们提供多个会话和一个鉴别器(通常是基于
    ThreadLocal
    )来选择合适的
    会话
    2.0的一些示例代码如下所示:

    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;
        }
    
        // …
    }
    
    类MyRoutingSessionFactory扩展了AbstractRoutingSessionFactory{ ThreadLocal lookupKey=ThreadLocal.withInitial(()->“默认会话”); void setLookupKey(字符串lookupKey){ this.lookupKey.set(lookupKey); } @凌驾 受保护的对象确定当前查找键(){ 返回lookupKey.get(); } } 类MyConfig扩展了AbstractCassandraConfiguration{ @豆子 @凌驾 public SessionFactory SessionFactory(){ MyRoutingSessionFactory=新的MyRoutingSessionFactory(); setDefaultTargetSessionFactory(getRequiredSession()); MapSessionFactoryLookup=新的MapSessionFactoryLookup(); 会话myOtherSession=…; addSessionFactory(“默认会话”,getRequiredSession()); addSessionFactory(“我的另一个会话”,myOtherSession); setSessionFactoryLookup(查找); 返回工厂; } // … }
    您能详细分享一下您想要实现的目标吗?主要的想法是,我希望有某种机制,使我能够只对我选择的一些键空间进行查询(选择、插入、更新、删除),而通过阅读文档,我似乎无法做到这一点,这就是我在这里寻求帮助的原因。Atm我使用
    org.springframework.data.cassandra.repository.CassandraRepository
    以及
    org.springframework.stereotype.repository
    ,这种机制不允许我达到我想要的灵活性水平。我应该使用多个会话吗(只有在这里我可以在单个会话上设置键空间)并在我的Java代码中管理它?对于像Cassandra这样的db来说,这种方法似乎非常麻烦,因为它保证了很大的灵活性。因此,给定查询A和一组键空间(K1、K2、K3、K4、K5等),我希望查询A只在K1和K4上执行。我可以通过使用来自Sping数据的API以某种方式实现这一点吗?您可以详细分享您想要实现的吗?主要思想是我希望有某种机制,使我能够执行查询(选择、插入、更新、删除)只有在我选择的一些键空间上,通过阅读文档,我似乎无法做到这一点,这就是为什么我在这里寻求帮助。Atm我使用
    org.springframework.data.cassandra.repository.CassandraRepository
    org.springframework.stereotype.repository
    一起使用,而这个机制不允许我这么做达到我想要的灵活性水平。我应该使用多个会话(只有在这里我可以在一个会话上设置键空间)并在Java代码中管理它吗?对于像Cassandra这样的数据库来说,这样的方法似乎非常麻烦,因为它保证了很大的灵活性。因此给定查询a和一组键空间(K1、K2、K3、K4、K5等),我希望查询A只在K1和K4上执行。我可以通过使用来自Sping数据的API以某种方式实现这一点吗?我在文档中看到了类似的东西(上周我就是这么做的),但上面的代码仍然让我遇到了相同的问题:我需要单独管理来自会话工厂的会话(每个会话都有自己的键空间)以实现给定的查询A和一组键空间(K1、K2、K3、K4、K5等),我希望查询A只在K1和K4上执行,因此上面的代码帮助我使用键空间K1创建会话S1,使用键空间K4创建会话S4,并使用这两个sees向它们发送相同的查询Q,以便更新K1和K4。会话对象的一个急需配置是
    会话s=新会话(K1,K4);s.execute(Q)
    但这似乎是不可能的,至少目前是不可能的。不可能在多个按键上同时/串行运行单个查询