无法使用带有弹簧数据的NetworkReplication创建cassandra键空间

问题描述 投票:0回答:1

我有两个键空间。其中一个完美创建,而另一个仅通过SimpleReplication创建。当我通过网络复制创建它时,会创建它,但是当我这样做时描述cqlsh中的键空间,它会返回:

'NoneType'对象没有属性'export_for_schema'

但是可以从cqlsh创建具有网络复制的密钥空间。在春季日志中,一切看起来都不错

 [CREATE KEYSPACE IF NOT EXISTS a_events_local WITH replication = { 'class' : 'NetworkTopologyStrategy', 'dс1' : 3, 'dс2' : 3 }

我的Cassandra配置

public abstract class CassandraConfig extends AbstractCassandraConfiguration {

    @Value("${cassandra.host}")
    private String host;

    @Value("${cassandra.port}")
    private int port;

    @Value("${cassandra.user}")
    private String usr;

    @Value("${cassandra.psswd}")
    private String psswd;

    @Value("${cassandra.keyspace.a}")
    private String keyspaceA;

    @Value("${cassandra.keyspace.b}")
    private String keyspaceB;


    @Override
    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster =
                new CassandraClusterFactoryBean();
        cluster.setContactPoints(host);
        cluster.setPort(port);
        cluster.setUsername(usr);
        cluster.setPassword(psswd);
        cluster.setKeyspaceCreations(getKeyspaceCreations());
        cluster.setJmxReportingEnabled(false);

        return cluster;
    }


    @Override
    protected String getContactPoints() {
        return host;
    }

    @Override
    protected int getPort() {
        return port;
    }

    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.NONE;
    }

    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
        return Arrays.asList(getAKeySpaceSpecification(), getBKeySpaceSpecification());
    }

    private CreateKeyspaceSpecification getAKeySpaceSpecification() {
        return CreateKeyspaceSpecification.createKeyspace(keyspaceA)
                .ifNotExists(true)
                //.withSimpleReplication(3);
                .withNetworkReplication(DataCenterReplication.of("dс1", 3) , DataCenterReplication.of("dс2", 3));
    }

    private CreateKeyspaceSpecification getBKeySpaceSpecification() {
        return CreateKeyspaceSpecification.createKeyspace(keyspaceB)
                .ifNotExists(true)
                .withNetworkReplication(DataCenterReplication.of("dc1", 3), DataCenterReplication.of("dc2", 3));
    }

键空间A配置

@Configuration
@EnableCassandraRepositories(
        cassandraTemplateRef = "keyspaceACassandraTemplate")
public class CassandraDDConfig extends CassandraConfig {

    @Value("${cassandra.keyspace.a}")
    private String keyspace;

    @Value("${cassandra.a-entities-package}")
    private String aEntityPackage;

    @Override
    @Bean("keyspaceDDSession")
    public CassandraSessionFactoryBean session() {
        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;
    }

    @Override
    @Bean("keyspaceACassandraTemplate")
    public CassandraAdminTemplate cassandraTemplate() throws Exception {
        return new CassandraAdminTemplate(this.session().getObject(), cassandraConverter());
    }


    @Override
    protected String getKeyspaceName() {
        return keyspace;
    }

    @Override
    public String[] getEntityBasePackages() {
        return new String[] {aEntityPackage};
    }
}

键空间B配置

@Configuration
@EnableCassandraRepositories(
        cassandraTemplateRef = "keyspaceBCassandraTemplate")
public class CassandraSparkConfig extends CassandraConfig {

    @Value("${cassandra.keyspace.b}")
    private String keyspace;

    @Value("${cassandra.b-entities-package}")
    private String dcEntityPackage;


    @Override
    @Bean("keyspaceBSession")
    public CassandraSessionFactoryBean session() {
        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;
    }

    @Override
    @Bean("keyspaceSparkCassandraTemplate")
    public CassandraAdminTemplate cassandraTemplate() throws Exception {
        return new CassandraAdminTemplate(this.session().getObject(), cassandraConverter());
    }

    @Override
    protected String getKeyspaceName() {
        return keyspace;
    }

    @Override
    public String[] getEntityBasePackages() {
        String[] entities = new String[1];
        if(saveAll){
            entities[0] = bEntityPackage;
        }
        return entities;
    }
}
cassandra spring-data-cassandra
1个回答
1
投票

最后,我使用getStartupScripts创建键空间。它可以正常工作,数据将写入相应的表中或从中读取。

@Override
    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster =
                new CassandraClusterFactoryBean();
        cluster.setContactPoints(host);
        cluster.setPort(port);
        cluster.setUsername(usr);
        cluster.setPassword(psswd);
        cluster.setStartupScripts(getStartupScripts());
        cluster.setJmxReportingEnabled(false);
        cluster.setMetricsEnabled(false);


@Override
    protected List<String> getStartupScripts() {
        final String scriptCreateAkeyspace =
                "CREATE KEYSPACE IF NOT EXISTS "
                        + keyspaceA
                        + " WITH durable_writes = true"
                        + " AND replication = {'class' : 'NetworkTopologyStrategy', 'dc1' : 3, 'dc2' : 1 };";

        final String scriptCreateSparkKeyspace = "CREATE KEYSPACE IF NOT EXISTS "
                + keyspaceB
                + " WITH durable_writes = true"
                + " AND replication = {'class' : 'NetworkTopologyStrategy', 'dc1' : 1, 'dc2' : 3 };";

        return Arrays.asList(scriptCreateAkeyspace, scriptCreateBKeyspace);
    }
© www.soinside.com 2019 - 2024. All rights reserved.