Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

SN avatar image
SN asked SN commented

How can we create a keyspace with IF NOT EXISTS in Spring Data using XML config?

We are migrating to latest Spring Data Cassandra 3.3.1. One challenge we are noticing is to create Keyspace if not exists using XML config. Not sure if this is supported in the latest version.

Could anybody share how we can achieve this using XML config?

Thanks

spring-data-cassandra
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez edited

I found this example on docs.spring.io:

<cql:cluster>
  <cql:startup-cql><![CDATA[
CREATE KEYSPACE IF NOT EXISTS my_other_keyspace WITH durable_writes = true AND replication = { 'replication_factor' : 1, 'class' : 'SimpleStrategy' };
    ]]></cql:startup-cql>
  <cql:shutdown-cql><![CDATA[
DROP KEYSPACE my_other_keyspace;
    ]]></cql:shutdown-cql>
</cql:cluster>

but I'm not sure about whether it's supported in specific Spring Data versions. Cheers!

1 comment Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks a lot for your comments. The solution you advised is not supported in latest Spring Data Cassandra ver 3.3.1. I am trying to reach out to them. Coming to AWS Keyspace, while creating this issue, there is a mandatory field called Topic and it lists only two options. (AWS Keyspace / System keyspace) Randomly I selected one as both of them are not relevant for me.


Once again thanks for your quick response !!

0 Likes 0 ·
Cedrick Lunven avatar image
Cedrick Lunven answered SN commented

Hi,

Personal Advice: Please do not use this spring-data feature. Keyspace and Schema creations from spring-data lead to invalid data model as people tend to reuse the same objects for multiple queries on the same data - eventually using the forbidden ALLOW FILTERING. Start with a proper design of your model, (1 table = 1 query in 90% of cases) ... and only then start coding.

I see 2 ways to perform the create keyspace operation using the Spring Cassandra namespace in the XML.

<cassandra:script=...
<cassandra:keyspace=...

Now the cassandra:keyspace will try to create the keyspace each time even with the action CREATE_DROP and I did not see any other action available in the KeyspaceAction. So better go with the script.


I created a github repository for you:

https://github.com/clun/spring-data-xml

Follow the instructions :

  • Clone the repo
git clone https://github.com/clun/spring-data-xml.git
cd spring-data-xml
  • Start Cassandra container
docker-compose up -d
  • I like to have a cqlSH to know what is happening. In a new terminal
docker run --rm -it -e CQLSH_HOST=localhost --net=host nuvo/docker-cqlsh bash
cqlsh
describe keyspaces
  • Start application
mvn spring-boot:run
  • Check your keyspaces again you have now 2 new keyspaces ks1 and ks2
describe keyspaces

Best Regards





1 comment Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Thanks a lot for sharing the (git) details. Only issue with script is that the replication in Keyspace definition is not customizable.

0 Likes 0 ·