如何使用spring数据cassandra创建表?

时间:2014-08-18 06:22:27

标签: java spring cassandra spring-data-cassandra

我创建了自己的存储库:

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

那么如何自动创建cassandra表的问题呢?目前Spring注入了MyRepository,试图将实体插入到不存在的表中。

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

PS 如果只有config boolean属性而不添加xml行并创建类似 BeanFactory 等等,那将是非常好。 : - )

4 个答案:

答案 0 :(得分:8)

覆盖AbstractCassandraConfiguration类

上的getSchema Action属性
@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());
    }

}

答案 1 :(得分:3)

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

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

答案 2 :(得分:3)

  1. 您需要在spring-data-cassandra文件中加入pom.xml依赖项。
  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 文件中的 Keyspace 联络点端口。喜欢:

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

答案 3 :(得分:0)

在application.properties文件中使用它

spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS