10

我已经像这样创建了自己的存储库:

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

那么问题是如何为此自动创建 cassandra 表?目前 Spring 注入MyRepository尝试将实体插入到不存在的表中。

那么有没有办法在 spring 容器启动期间创建 cassandra 表(如果它们不存在)?

PS如果只有配置布尔属性而不添加xml行并创建像BeanFactory等这样的东西,那就太好了。:-)

4

5 回答 5

11

覆盖 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());
    }

}
于 2014-08-18T11:55:09.557 回答
10

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

spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS
于 2019-05-13T07:37:14.623 回答
5

您还需要在 AbstractCassandraConfiguration 实现中覆盖 getEntityBasePackages() 方法。这将允许 Spring 找到您使用 @Table 注释的任何类,并创建表。

@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.example"};
}
于 2015-05-05T21:07:58.420 回答
4
  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方法可能是可选的。试试没有这个。

  3. 确保.properties文件中的Keyspacecontactpointsport 。喜欢 :

    cassandra.contactpoints=localhost,127.0.0.1
    cassandra.port=9042 
    cassandra.keyspace='Your Keyspace name here'
    
于 2017-10-06T17:07:16.460 回答
1

实际上,在深入研究位于 中的源代码后spring-data-cassandra:3.1.9,您可以检查实现:

org.springframework.data.cassandra.config.SessionFactoryFactoryBean#performSchemaAction

实施如下:

protected void performSchemaAction() throws Exception {

    boolean create = false;
    boolean drop = DEFAULT_DROP_TABLES;
    boolean dropUnused = DEFAULT_DROP_UNUSED_TABLES;
    boolean ifNotExists = DEFAULT_CREATE_IF_NOT_EXISTS;

    switch (this.schemaAction) {
        case RECREATE_DROP_UNUSED:
            dropUnused = true;
        case RECREATE:
            drop = true;
        case CREATE_IF_NOT_EXISTS:
            ifNotExists = SchemaAction.CREATE_IF_NOT_EXISTS.equals(this.schemaAction);
        case CREATE:
            create = true;
        case NONE:
        default:
            // do nothing
    }

    if (create) {
        createTables(drop, dropUnused, ifNotExists);
    }
}

这意味着您必须分配CREATEschemaAction是否从未创建过表。并且CREATE_IF_NOT_EXISTS剂量不起作用。


更多信息请在此处查看:为什么带有 `spring-data-cassandra` 的`spring-data-jpa` 不会自动创建 cassandra 表?

于 2021-09-16T03:49:20.153 回答