Create Spring.Data.Cassandra keyspace in runtime

xmlParser

I am using Cassandra in my Spring Boot app. When I start the app, the keyspace is automatically created and everything is okay. The problem is that I want to create keyspace in runtime. For example from endpoint or from application.properties. I have tried only with @RefreshScope and after that with

spring.cloud.refresh.extra-refreshable=spring.data.cassandra.keyspace-name

and sent POST on actuator/refresh but both solutions does not work. The Class is simple Person class with @Table annotation.

So my question is: Is it possible to create keyspace in runtime after sending request to actuator/refresh???

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>

And this is what I have in application.properties

#spring.data.cassandra.keyspace-name=test1_keyspace
#spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS
#spring.data.cassandra.contact-points=IP
Bambus

You can achieve that with this:

@Component
class MySessionRefresh {

  @Autowired
  Environment env;

  @Autowired
  Session session;

  // omitted constructors for brevity

  @EventListener
  @Order(Ordered.LOWEST_PRECEDENCE)
  public void handle(RefreshScopeRefreshedEvent event) {

    String keyspace = env.getProperty("spring.data.cassandra.keyspace-name");
    session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 };");
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Create keyspace, table and generate tables dynamically using Spring Data Cassandra

Change of Cassandra Keyspace at runtime

How to set Spring Data Cassandra keyspace dynamically?

how to create a keyspace in cassandra?

Spring data reactive Cassandra not creating keyspace on startup - InvalidQueryException: Keyspace 'xxx' does not exist

Cannot create keyspace with cassandra with php

Cassandra: customer data per keyspace

Create a duplicate of an existing Keyspace in Cassandra (with a new name)

How to create a keyspace in cassandra using gocql

Create keyspace automatically inside docker container with cassandra

keyspace and tables not replicating across data centre in cassandra

Cassandra keyspace disappeared leading to data loss

Cassandra: Unable to read keyspace from data directory

How to create custom conversions for Spring Data Cassandra?

will cassandra fail on two parallel create keyspace commands executed simultanously

Create Cassandra Materialized view on different keyspace fail - unconfigured table

Spring Cassandra: Creating Schema in run time in another keyspace

Spring Boot Astra Cassandra - keyspace specified in application.yml but still throws error "No keyspace has been specified"

How to rename keyspace in Cassandra?

cassandra keyspace with capital letter

Cassandra Keyspace name with hyphen (-)

Cassandra Copy KeySpace - How To?

Cassandra Drop Keyspace Snapshot Cleaning

Multiple keyspace or table update for cassandra

Cassandra - Keyspace level Multi Tenancy

Spring Data Cassandra and PreparedStatementCache

Spring Boot Data Embedded Cassandra

Spring data Cassandra, Allow filtering

Spring Data Cassandra LocalDateTime Conversion