简体   繁体   English

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

[英]How create table with spring data cassandara?

I have created my own repository like that:我已经像这样创建了自己的存储库:

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

So the question how automatically create cassandra table for that?那么问题是如何为此自动创建 cassandra 表? Currently Spring injects MyRepository which tries to insert entity to non-existent table.目前 Spring 注入了MyRepository ,它试图将实体插入到不存在的表中。

So is there a way to create cassandra tables (if they do not exist) during spring container start up?那么有没有办法在 spring 容器启动期间创建 cassandra 表(如果它们不存在)?

PS It would be very nice if there is just config boolean property without adding lines of xml and creation something like BeanFactory and etc. :-) PS如果只有配置布尔属性而不添加 xml 行和创建诸如BeanFactory 之类的东西,那将会非常好:-)

Overide the getSchemaAction property on the AbstractCassandraConfiguration class覆盖 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

You'll also need to Override the getEntityBasePackages() method in your AbstractCassandraConfiguration implementation.您还需要覆盖 AbstractCassandraConfiguration 实现中的getEntityBasePackages()方法。 This will allow Spring to find any classes that you've annotated with @Table, and create the tables.这将允许 Spring 找到您使用 @Table 注释的任何类,并创建表。

@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.example"};
}
  1. You'll need to include spring-data-cassandra dependency in your pom.xml file.您需要在pom.xml文件中包含spring-data-cassandra依赖项。
  2. Configure your TestConfig.class as below:配置您的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()); } }

    This last getInitialEntitySet method might be an Optional one.最后一个getInitialEntitySet方法可能是一个 Optional 方法。 Try without this too.也试试没有这个。

  3. Make sure your Keyspace , contactpoints and port in .properties file.确保您的Keyspacecontactpointsport.properties文件中。 Like :喜欢 :

     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