How to create a table with spring cassandara data?

I created my own repository:

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

So the question is, how to automatically create a cassandra table for this? Spring is currently introducing MyRepository , which is trying to insert an entity into a nonexistent table.

So, is there a way to create cassandra tables (if they do not exist) while starting the Spring container?

PS It would be very nice if there is only a configuration logical property without adding xml strings and creating something like BeanFactory, etc .:-)

+6
source share
4 answers

Top of the getSchemaAction property in 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()); } } 
+8
source

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

 @Override public String[] getEntityBasePackages() { return new String[]{"com.example"}; } 
+3
source
  • You need to include the spring-data-cassandra dependency in pom.xml .
  • Configure your TestConfig.class as shown 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 final getInitialEntitySet method may be optional. Try without it.

  • Make sure your keyspace, contact points, and port are in the .properties file. For instance:

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

Use this in application.properties file

 spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS 
0
source

Source: https://habr.com/ru/post/973954/


All Articles