如何初始化密钥空间卡桑德拉和表格与Spring启动

问题描述 投票:5回答:4

我使用卡桑德拉在我的春节,启动应用程序中的数据源,并想在应用程序启动之前初始化数据库。

到现在为止我所做的是,我已经定义了一类“CassandraConfiguration”扩展“AbstractCassandraConfiguration”类中,你可以看到下面的例子,我有延长“CassandraRepository”库。当我创建密钥空间和表自己的应用程序工作正常。

不过,我想自动创建密钥空间和表而应用程序正在启动。为了做到这一点,我提供的资源文件夹下的schema.cql文件,但我不能让这个脚本的工作。

没有人有任何想法,我能做些什么来自动创建密钥空间(S)和表?

谢谢。

编辑:我使用卡桑德拉2.0.9,弹簧引导1.3.2.RELEASE和datastax卡桑德拉驱动程序2.1.6版本。

Cassandra configuration.Java

@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
@EnableCassandraRepositories(basePackages = { "bla.bla.bla.repository" })
public class CassandraConfiguration extends AbstractCassandraConfiguration {

    @Autowired
    private Environment environment;


    @Bean
    public CassandraClusterFactoryBean cluster() {
        CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
        cluster.setContactPoints( environment.getProperty( "cassandra.contactpoints" ) );
        cluster.setPort( Integer.parseInt( environment.getProperty( "cassandra.port" ) ) );
        return cluster;
    }


    @Bean
    public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
        return new BasicCassandraMappingContext();
    }


    @Bean
    public CassandraConverter converter() throws ClassNotFoundException {
        return new MappingCassandraConverter(cassandraMapping());
    }


    @Override
    protected String getKeyspaceName() {
        return environment.getProperty( "cassandra.keyspace" );
    }


    @Bean
    public CassandraSessionFactoryBean session() throws Exception {

        CassandraSessionFactoryBean session = new CassandraSessionFactoryBean();
        session.setCluster(cluster().getObject());
        session.setKeyspaceName(environment.getProperty("cassandra.keyspace"));
        session.setConverter(converter());
        session.setSchemaAction(SchemaAction.NONE);

        return session;
    }


    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.RECREATE_DROP_UNUSED;
    }
}
cassandra spring-boot spring-data-cassandra
4个回答
5
投票

如果你仍然有这个问题,在春季启动2和SD卡珊德拉2.0.3你可以做到这一点简单的Java配置和设置的一切开箱。

@Configuration
@EnableCassandraRepositories(basePackages = "com.example.repository")
public class DbConfigAutoStart extends AbstractCassandraConfiguration {

    /*
     * Provide a contact point to the configuration.
     */
    @Override
    public String getContactPoints() {
        return "exampleContactPointsUrl";
    }

    /*
     * Provide a keyspace name to the configuration.
     */
    @Override
    public String getKeyspaceName() {
        return "exampleKeyspace";
    }

    /*
     * Automatically creates a Keyspace if it doesn't exist
     */
    @Override
    protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
        CreateKeyspaceSpecification specification = CreateKeyspaceSpecification
                .createKeyspace("exampleKeyspace").ifNotExists()
                .with(KeyspaceOption.DURABLE_WRITES, true).withSimpleReplication();
        return Arrays.asList(specification);
    }


    /*
     * Automatically configure a table if doesn't exist
     */
    @Override
    public SchemaAction getSchemaAction() {
        return SchemaAction.CREATE_IF_NOT_EXISTS;
    }


    /*
     * Get the entity package (where the entity class has the @Table annotation)
     */
    @Override
    public String[] getEntityBasePackages() {
        return new String[] { "com.example.entity" };
    }

你是好去


3
投票

你的返回类型BasicCassandraMappingContext()可能会被弃用。采用

@Bean
public CassandraMappingContext mappingContext() throws ClassNotFoundException {
    CassandraMappingContext mappingContext= new CassandraMappingContext();
    mappingContext.setInitialEntitySet(getInitialEntitySet());
    return mappingContext;
}
@Override
public String[] getEntityBasePackages() {
    return new String[]{"base-package name of all your entity, annotated 
with @Table"};
}

@Override
protected Set<Class<?>> getInitialEntitySet() throws ClassNotFoundException {
    return CassandraEntityClassScanner.scan(getEntityBasePackages());
}

代替,

@Bean
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
    return new BasicCassandraMappingContext();
}

还设置:

session.setSchemaAction(SchemaAction.RECREATE_DROP_UNUSED);

并排除:

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

得到参考here


1
投票

我与spring-boot 1.5.10.RELEASEcassandra 3.0.16工作,但你可以尝试缩减版本。要创建密钥空间从你application.ymlapplication.properties你可以导入keyspacename。使用您的表应该生成自动提供您已设置的实体基础包@Table注解。

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

@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.example.your.entities"};
}

@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
    return Arrays.asList(
            CreateKeyspaceSpecification.createKeyspace()
                    .name(keySpace)
                    .ifNotExists()
    );
}

0
投票

最后我得到它通过增加setKeyspaceCreations(getKeyspaceCreations())到CassandraClusterFactoryBean覆盖,并且确保启用@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));
}



}
© www.soinside.com 2019 - 2024. All rights reserved.