簡體   English   中英

如何使用 spring 數據 cassandara 創建表?

[英]How create table with spring data cassandara?

我已經像這樣創建了自己的存儲庫:

public interface MyRepository extends TypedIdCassandraRepository<MyEntity, String> {
}

那么問題是如何為此自動創建 cassandra 表? 目前 Spring 注入了MyRepository ,它試圖將實體插入到不存在的表中。

那么有沒有辦法在 spring 容器啟動期間創建 cassandra 表(如果它們不存在)?

PS如果只有配置布爾屬性而不添加 xml 行和創建諸如BeanFactory 之類的東西,那將會非常好:-)

覆蓋 AbstractCassandraConfiguration 類上的 getSchemaAction 屬性

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

    @Override
    public String getKeyspaceName() {
        return "test_config";
    }

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

    @Bean
    public CassandraOperations cassandraOperations() throws Exception {
        return new CassandraTemplate(session().getObject());
    }

}

您可以在application.properties 中使用此配置

spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS

您還需要覆蓋 AbstractCassandraConfiguration 實現中的getEntityBasePackages()方法。 這將允許 Spring 找到您使用 @Table 注釋的任何類,並創建表。

@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.example"};
}
  1. 您需要在pom.xml文件中包含spring-data-cassandra依賴項。
  2. 配置您的TestConfig.class如下:

     @Configuration @PropertySource(value = { "classpath:Your .properties file here" }) @EnableCassandraRepositories(basePackages = { "base-package name of your Repositories'" }) public class CassandraConfig { @Autowired private Environment environment; @Bean public CassandraClusterFactoryBean cluster() { CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean(); cluster.setContactPoints(env.getProperty("contactpoints from your properties file")); cluster.setPort(Integer.parseInt(env.getProperty("ports from your properties file"))); return cluster; } @Bean public CassandraConverter converter() { return new MappingCassandraConverter(mappingContext()); } @Bean public CassandraSessionFactoryBean session() throws Exception { CassandraSessionFactoryBean session = new CassandraSessionFactoryBean(); session.setCluster(cluster().getObject()); session.setKeyspaceName(env.getProperty("keyspace from your properties file")); session.setConverter(converter()); session.setSchemaAction(SchemaAction.CREATE_IF_NOT_EXISTS); return session; } @Bean public CassandraOperations cassandraTemplate() throws Exception { return new CassandraTemplate(session().getObject()); } @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()); } }

    最后一個getInitialEntitySet方法可能是一個 Optional 方法。 也試試沒有這個。

  3. 確保您的Keyspacecontactpointsport.properties文件中。 喜歡 :

     cassandra.contactpoints=localhost,127.0.0.1 cassandra.port=9042 cassandra.keyspace='Your Keyspace name here'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM