How create table with spring data cassandara?

2019-01-25 15:30发布

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? Currently Spring injects MyRepository which tries to insert entity to non-existent table.

So is there a way to create cassandra tables (if they do not exist) during spring container start up?

P.S. It would be very nice if there is just config boolean property without adding lines of xml and creation something like BeanFactory and etc. :-)

3条回答
唯我独甜
2楼-- · 2019-01-25 15:35

Overide the getSchemaAction property on the AbstractCassandraConfiguration class

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

}
查看更多
Lonely孤独者°
3楼-- · 2019-01-25 15:36

You'll also need to Override the getEntityBasePackages() method in your AbstractCassandraConfiguration implementation. This will allow Spring to find any classes that you've annotated with @Table, and create the tables.

@Override
public String[] getEntityBasePackages() {
    return new String[]{"com.example"};
}
查看更多
4楼-- · 2019-01-25 15:44
  1. You'll need to include spring-data-cassandra dependency in your pom.xml file.
  2. Configure your TestConfig.class as below:

    @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. Try without this too.

  3. Make sure your Keyspace, contactpoints and port in .properties file. Like :

    cassandra.contactpoints=localhost,127.0.0.1
    cassandra.port=9042 
    cassandra.keyspace='Your Keyspace name here'
    
查看更多
登录 后发表回答