How create table with spring data cassandara?

Related searches

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. :-)

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

}

Create keyspace, table and generate tables dynamically using , method on the CassandraClusterFactoryBean . For generating the application's Tables, you essentially just need to annotate your application domain object(s) (entities) using the SD Cassandra @Table annotation, and make sure your domain objects/entities can be found on the application's CLASSPATH. Using Cassandra, I want to create keyspace and tables dynamically using Spring Boot application. I am using Java based configuration. I have an entity annotated with @Table whose schema I want to be created before application starts up since it has fixed fields that are known beforehand.

You can use this config in the application.properties

spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS

Spring Data for Apache Cassandra, Update.create().increment("balance", 50.00), Account.class); a selection of objects in the Apache Cassandra table. – @Table: identifies a domain object to be persisted to Cassandra as a table. – @PrimaryKey: identifies the primary key field of the entity. 4. Create a Cassandra repository – Open application.properties, configure spring.data.cassandra:

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"};
}

Getting started with Spring Data Cassandra, Since I am using Spring Boot in this post spring-boot-starter-data-cassandra can to allow Spring to create tables that do not exist if there is a entity with @Table � Today we’ve built a Rest CRUD API using Spring Boot, Spring Data Cassandra and Spring Web MVC to create, retrieve, update, delete documents in Cassandra database. We also see that CassandraRepository supports a great way to make CRUD operations and custom finder methods without need of boilerplate code.

  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'
    

Identifier.java. Note the use of the @UserDefinedType annotation.. Entities. An entity is a Java class that is mapped to a Cassandra table. The class includes all of the table’s columns or

The learning curve for developing applications with Apache Cassandra is significantly reduced when using Spring Data for Apache Cassandra. With the power to stay at a high level with annotated POJOs, or at a low level with high performance data ingestion capabilities, the Spring Data for Apache Cassandra templates are sure to meet every application need.

Spring Data for Apache Cassandra approaches data access with mapped entity classes that fit your data model. You can use these entity classes to create Cassandra table specifications and user type definitions.

INSERT, SELECT, DELETE DATA CASSANDRA USING SPRING DATA. In this article we will cover in detail how we can use the Cassandra Spring Data, for manipulation of the data in a spring boot application. While keeping the Spring as our Cassandra connection manager and data manager by utilizing the magic of the Spring.

Comments
  • I'm having the exact same issue and would love to know. So far I've created a custom repository method called "createIfExists" where I've instantiated a CreateTableSpecification with ifNotExists() called on it. I manually call the method after spring-data-cassandra initialization.
  • I'm creating the SessionBeanFactory manually (to override spring-boot's) and setting the schema action. However, it doesn't appear to have made any difference and I still get "InvalidQueryException: unconfigured table mytable".
  • I suppose presently spring-data-cassandra 1.5M1 it is not working. Can you confirm on.
  • @AllahbakshAsadullah yes its not working with version 1.5.1. What version did you use instead?