Spring数据响应式Cassandra在启动时未创建密钥空间-InvalidQueryException:密钥空间“ xxx”不存在

时间:2019-02-05 13:18:15

标签: spring-data reactive-programming reactive spring-data-cassandra

我正在使用弹簧数据反应式Cassandra。下面给出的是我的配置类。我的期望是,在春季启动应用程序加载时,它必须连接到Cassandra并创建密钥空间。但是我的应用程序能够连接到Cassandra,但是没有创建键空间。我在启动时遇到以下异常。请让我知道我是否缺少任何东西。

import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.AbstractReactiveCassandraConfiguration;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.DropKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption;
import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories;

import java.util.Arrays;
import java.util.List;



@Configuration
@EnableReactiveCassandraRepositories
public class CassandraConfig extends AbstractReactiveCassandraConfiguration {
    @Value("${spring.data.cassandra.contactpoints}") private String contactPoints;
    @Value("${spring.data.cassandra.port}") private int port;
    @Value("${spring.data.cassandra.keyspace-name}") private String keyspace;

    @Value("${spring.data.cassandra.username}") private String userName;
    @Value("${spring.data.cassandra.password}") private String password;
    @Value("${cassandra.basepackages}") private String basePackages;


    @Override protected String getKeyspaceName() {
        return keyspace;
    }
    @Override protected String getContactPoints() {
        return contactPoints;
    }
    @Override protected int getPort() {
        return port;
    }
    @Override public SchemaAction getSchemaAction() {
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    }
    @Override public String[] getEntityBasePackages() {
        return new String[] {
                basePackages
        };
    }

    @Override
    public CassandraClusterFactoryBean cluster() {
        PlainTextAuthProvider authProvider = new PlainTextAuthProvider(userName, password);

        CassandraClusterFactoryBean cluster=new CassandraClusterFactoryBean();

        cluster.setJmxReportingEnabled(false);
        cluster.setContactPoints(contactPoints);
        cluster.setPort(port);
        cluster.setAuthProvider(authProvider);
        cluster.setReconnectionPolicy(new ConstantReconnectionPolicy(1000));

        return cluster;
    }



    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {

        CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(keyspace)
                .ifNotExists()
                .with(KeyspaceOption.DURABLE_WRITES, true);

        return Arrays.asList(specification);
    }


    @Override
    protected List<DropKeyspaceSpecification> getKeyspaceDrops() {
        return Arrays.asList(DropKeyspaceSpecification.dropKeyspace(keyspace));
    }

}

通过构造函数参数5表示的不满足的依赖关系;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建名称为“ myRepository”的bean时出错:在设置bean属性“ reactiveCassandraOperations”时无法解析对bean“ reactiveCassandraTemplate”的引用;嵌套的异常是org.springframework.beans.factory.BeanCreationException:创建com.company.domain.CassandraConfig中定义的名称为'reactiveCassandraTemplate'的bean时出错:通过工厂方法实例化Bean失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.cassandra.core.ReactiveCassandraTemplate]:工厂方法'reactiveCassandraTemplate'引发了异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:在com.company.domain.CassandraConfig中定义名称为'reactiveSessionFactory'的bean创建时出错:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.cassandra.ReactiveSessionFactory]:工厂方法'reactiveSessionFactory'引发了异常;嵌套的异常是org.springframework.beans.factory.BeanCreationException:在com.company.domain.CassandraConfig中定义名称为'reactiveSession'的bean创建时出错:通过工厂方法进行bean实例化失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.data.cassandra.ReactiveSession]:工厂方法'reactiveSession'抛出异常;嵌套的异常是org.springframework.beans.factory.BeanCreationException:在com.company.domain.CassandraConfig中创建名称为“ session”的bean时出错:调用init方法失败;嵌套的异常是com.datastax.driver.core.exceptions.InvalidQueryException:键空间“ mykeyspace”不存在

1 个答案:

答案 0 :(得分:0)

最后通过以下代码对其进行了修复。我们需要在CassandraClusterFactoryBean上设置setKeyspaceCreations并启用@ComponentScan。

import com.datastax.driver.core.PlainTextAuthProvider;
import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.*;
import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.DropKeyspaceSpecification;
import org.springframework.data.cassandra.core.cql.keyspace.KeyspaceOption;
import org.springframework.data.cassandra.repository.config.EnableReactiveCassandraRepositories;

import java.util.Arrays;
import java.util.List;



@Configuration
@EnableReactiveCassandraRepositories(basePackages = "com.company.domain.data")
public class CassandraConfig extends AbstractReactiveCassandraConfiguration{
@Value("${spring.data.cassandra.contactpoints}") private String contactPoints;
@Value("${spring.data.cassandra.port}") private int port;
@Value("${spring.data.cassandra.keyspace-name}") private String keyspace;

@Value("${spring.data.cassandra.username}") private String userName;
@Value("${spring.data.cassandra.password}") private String password;
@Value("${cassandra.basepackages}") private String basePackages;


@Override protected String getKeyspaceName() {
    return keyspace;
}
@Override protected String getContactPoints() {
    return contactPoints;
}
@Override protected int getPort() {
    return port;
}
@Override public SchemaAction getSchemaAction() {
    return SchemaAction.CREATE_IF_NOT_EXISTS;
}
@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.company.domain.data"};
}

@Override
public CassandraClusterFactoryBean cluster() {
    PlainTextAuthProvider authProvider = new PlainTextAuthProvider(userName, password);

    CassandraClusterFactoryBean cluster=new CassandraClusterFactoryBean();

    cluster.setJmxReportingEnabled(false);
    cluster.setContactPoints(contactPoints);
    cluster.setPort(port);
    cluster.setAuthProvider(authProvider);
    cluster.setKeyspaceCreations(getKeyspaceCreations());
    cluster.setReconnectionPolicy(new ConstantReconnectionPolicy(1000));

    return cluster;
}


@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {

    CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(keyspace)
            .ifNotExists()
            .with(KeyspaceOption.DURABLE_WRITES, true);

    return Arrays.asList(specification);
}



@Override
protected List<DropKeyspaceSpecification> getKeyspaceDrops() {
    return Arrays.asList(DropKeyspaceSpecification.dropKeyspace(keyspace));
}



}