1

I am trying to build a very simple api connecting to an astra Cassandra nodal database with spring boot. However, even when I have specified the keyspace in application.yml, it still throws the exception:

com.datastax.oss.driver.api.core.servererrors.InvalidQueryException: No keyspace has been specified. USE a keyspace, or explicitly specify keyspace.tablename

Here is my application.yml:

astra.api:
  application-token: <my-token>
  database-id: <my-db-id>
  database-region: <my-db-region>

astra.cql:
  enabled: true
  downloadScb.path: secure-connect.zip

spring.data.cassandra:
  keyspace-name: shopping_list
  schema-action: CREATE_IF_NOT_EXISTS

I have also tried using keyspace instead of keyspace-name, and putting it under astra.api or even astra.cql. None of these works, though.

I am following this tutorial exactly but I cannot figure out what is the problem.

I have checked the token, database-id and database-region are all correct and the secure connect bundle is also there. The application can indeed get the other values from application.yml such as the scb path and schema-action but apparently not keyspace-name.

Thanks a lot for any input!

Dan
  • 3,647
  • 5
  • 20
  • 26
Penthrite
  • 13
  • 4

1 Answers1

3

The astra-spring-boot-starter (with all key astra.*) will initialize CqlSession and any connection needed for Astra (Cassandra, Rest Apis, gRPC, pulsar...).

Spring Data Casandra detects the connection and uses it. You can use any configuration key of the Cassandra driver below driver-config.

The application.yaml should look like.

astra:
  api:
    application-token: <your_token>
    database-id: <your_db_id>
    database-region: <your_db_region>
  cql:
    enabled: true
    download-scb: 
    enabled: true
    driver-config:
      basic:
        session-keyspace: <your_keyspace>

Documentation of the starter https://github.com/datastax/astra-sdk-java/wiki/Spring-Boot-Starter-Quickstart#4-configuration

Sample working project (mvc, rest, cassandra, Oauth2, thymeleaf) https://github.com/datastaxdevs/workshop-betterreads

clunven
  • 1,360
  • 6
  • 13
  • 1
    Thank you so much! I have found like 10 different configurations from different websites and YouTube videos for the application.yaml (or application.properties) but this is the only one that actually works. – Penthrite May 27 '22 at 08:26
  • How do we add multiple keyspaces here ? as our application grew, we are thinking of using different keyspaces for each module. @clunven – Tuhin Subhra Mandal Sep 09 '22 at 06:32
  • Spring Data does not easily allow multiple keyspaces, you need to override template and session factory https://stackoverflow.com/questions/25770043/multiple-keyspace-support-for-spring-data-cassandra-repositories it is also a code smell as that probably means you need to break your monolithic (different keyspaces = different RF and DCs). You can also prefix your query with ks name: SELECT * from KS2.table – clunven Sep 09 '22 at 09:27