如何使用Spring Boot初始化Cassandra键空间和表

frk*_*rkn 6 cassandra spring-boot spring-data-cassandra

我在我的Spring Boot应用程序中将Cassandra用作数据源,并希望在应用程序启动之前初始化数据库。

到目前为止,我已经定义了扩展“ AbstractCassandraConfiguration”类的“ CassandraConfiguration”类,如下面的示例所示,并且我有一个扩展“ CassandraRepository”的存储库。当我自己创建键空间和表时,该应用程序运行良好。

但是,我想在应用程序启动时自动创建键空间和表。为此,我在资源文件夹下提供了一个schema.cql文件,但无法使该脚本正常工作。

有谁知道如何做才能自动创建键空间和表?

谢谢。

编辑:我正在使用Cassandra 2.0.9,spring-boot 1.3.2.RELEASE和datastax cassandra驱动程序2.1.6版本。

CassandraConfiguration.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;
    }
}
Run Code Online (Sandbox Code Playgroud)

Uro*_* T. 5

如果您仍然对此有疑问,请在Spring Boot 2和SD Cassandra 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" };
    }
Run Code Online (Sandbox Code Playgroud)

而且你很好走


小智 4

您的返回类型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());
}
Run Code Online (Sandbox Code Playgroud)

代替,

@Bean
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
    return new BasicCassandraMappingContext();
}
Run Code Online (Sandbox Code Playgroud)

还设置:

session.setSchemaAction(SchemaAction.RECREATE_DROP_UNUSED);
Run Code Online (Sandbox Code Playgroud)

并排除:

@Override
public SchemaAction getSchemaAction() {
    return SchemaAction.RECREATE_DROP_UNUSED;
}
Run Code Online (Sandbox Code Playgroud)

在这里获取参考。