如何使用Spring Boot初始化Cassandra键空间和表 [英] How to initialize Cassandra keyspace and tables with Spring boot
问题描述
我在我的Spring Boot应用程序中将Cassandra用作数据源,并希望在应用程序启动之前初始化数据库。
I am using Cassandra as a datasource in my Spring boot application and would like to initialize the database before the application starts.
到目前为止,我所做的是,如下面的示例所示,我定义了一个扩展了 AbstractCassandraConfiguration类的类 CassandraConfiguration,并且我有一个扩展了 CassandraRepository的存储库。当我自己创建键空间和表时,该应用程序运行良好。
Up to now what I have done is, I have defined a class "CassandraConfiguration" extending "AbstractCassandraConfiguration" class as in the examples which you can see below and I have a repository extending "CassandraRepository". When I create the keyspace and the table myself, the application works fine.
但是,我想在应用程序启动时自动创建键空间和表。为此,我在资源文件夹下提供了一个schema.cql文件,但无法使该脚本正常工作。
However, I want to create the keyspace and tables automatically while application is starting. In order to do that, I supplied a schema.cql file under resources folder but I could not make that script work.
有人知道我该怎么办吗?
Does anyone have any idea what can I do to create the keyspace(s) and tables automatically?
谢谢。
编辑:我正在使用Cassandra 2.0。 9,spring-boot 1.3.2.RELEASE和datastax cassandra驱动程序2.1.6版本。
I am using Cassandra 2.0.9, spring-boot 1.3.2.RELEASE and datastax cassandra driver 2.1.6 versions.
CassandraConfiguration.java
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;
}
}
推荐答案
如果您仍然对此有疑问,在 Spring Boot 2和SD Cassandra 2.0.3 中,您可以执行简单的Java配置并立即设置所有内容。
If you are still having problems with this, in Spring Boot 2 and SD Cassandra 2.0.3 you can do this straightforward Java configuration and setup everything out of the box.
@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" };
}
您很好!
这篇关于如何使用Spring Boot初始化Cassandra键空间和表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!