0

I am using Spring data Cassandra, to connect with Cassandra database, with configuration file extending AbstractCassandraConfiguration and overriding functions -

@Override
public SchemaAction getSchemaAction() {
    return SchemaAction.RECREATE_DROP_UNUSED;
}

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

My aim is to create tables automatically in Cassandra from the mentioned entities in com.example package with @Table annotation. For example -

package com.example;

import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

@Table(value="goal")
public class Goal {

    @PrimaryKey
    private int id;

    private String description;

    public Goal(int id, String description) {
        this.id = id;
        this.description = description;
    }

    public Goal() {

    }

    public int getId() {
        return id;
    }

    public String getDescription() {
        return description;
    }


    public void setId(int id) {
        this.id = id;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return "Goals [id=" + id + ", description=" + description + "]";
    }

}

For this entity, with the given configuration, one table should get created during spring initialization, But it fails to do so. No exception though, It just doesn't create anything in Cassandra. Any help would be appreciated. Thanks.

Bhushan
  • 123
  • 11
  • 1
    When using Spring Boot, this does not work out of the box yet. There's an open ticket (https://github.com/spring-projects/spring-boot/issues/4886) for this issue, maybe you want to cast your vote. – mp911de Apr 12 '16 at 15:40
  • 2
    Careful, generally it is a bad idea to let an application *automagically* generates schema for you. Schema and data model are crucial for the success of your apps with Cassandra so take care of it yourself – doanduyhai Apr 12 '16 at 19:29
  • Hi @mp911de , Thanks for the comment. But I think changing the **SchemaAction** property should work. I am already creating the bean of **BasicCassandraMappingContext**, So Ideally It should work. Is there any workaround to auto-create the tables from entities as of now ? – Bhushan Apr 13 '16 at 08:52
  • Thanks @doanduyhai for the suggestion, But I am using this for development and testing purpose, not for production. While moving my app to production, I will create scripts file to create and update the schema. – Bhushan Apr 13 '16 at 08:54
  • 1
    If you're using for test, consider switching to www.achilles.io. We have full support for JUnit with embedded Cassandra for testing. Disclose: I'm the creator of Achilles – doanduyhai Apr 13 '16 at 09:03
  • Thanks, @doanduyhai . Achilles is awesome. You have many features and customizations which make developers life easy. But I have a question here, does it slow down our application performance-wise? Because there are many customizations you have done on top of Cassandra. Also, Can we use **Achilles** in production also or this is for test purpose only? – Bhushan Apr 13 '16 at 13:00
  • Achilles is used in production by several companies. About performance, because Achilles **generates source code** at compile time, there is **no proxy** at runtime so the performance overhead is very small – doanduyhai Apr 13 '16 at 13:08
  • Thanks @doanduyhai for the details. I will try that. But I am still wondering why this is not working? – Bhushan Apr 18 '16 at 05:45
  • What do you mean by "this is not working" ? – doanduyhai Apr 18 '16 at 07:49
  • @doanduyhai, I meant whatever I am trying, the above code snippet in the question, why that is not working? Am I missing something? – Bhushan Apr 19 '16 at 08:46
  • No idea, maybe you can drop a question on the Spring Data Cassandra mailing list – doanduyhai Apr 20 '16 at 11:36

1 Answers1

0

I also faced similar requirement and following works for me:-

@Bean
public CassandraClusterFactoryBean cluster() {

    CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
    cluster.setContactPoints("127.0.0.1");
    cluster.setPort(9042);
    return cluster;
}    
public String[] getEntityBasePackages() {
    return new String[] { "com.user.entity" };
}

protected String getKeyspaceName() {
    return "user_db";
}

public SchemaAction getSchemaAction() {
    return SchemaAction.CREATE_IF_NOT_EXISTS;
}

Once your context is up, you can check the table in cassandra. It is assumed your keyspace is already created in cassandra. Please inform if you also want to create keyspace on the fly.

user7378545
  • 11
  • 1
  • 4
  • I am just trying to understand, How is this different from what I was trying. I tried - **SchemaAction.RECREATE_DROP_UNUSED;** And this didn't worked. Can you please explain this. – Bhushan Apr 26 '17 at 19:17