Manish Karki
Manish Karki

Reputation: 503

Multi-Keyspace with Cassandra's Java Driver on fly

Currently, We use Cassandra's Java driver to configure session based on the attributes defined in properties file which looks like this:

Cluster cluster = Cluster.builder()
            .addContactPoints(contactPoints)
            .withPort(environment.getRequiredProperty("cassandra.port", Integer.class))
            .withCredentials(username, password)
            .build();

Session session = cluster.connect(environment.getRequiredProperty("cassandra.keyspace"));

What we want to achieve now is to use multi-keyspace on fly, if there is a way to do it, being able to detect the cassandra connection for a given request without changing much on the existing logic except the configuration. Any suggestion or help on this would be a great way to start

Upvotes: 1

Views: 808

Answers (1)

Alex Ott
Alex Ott

Reputation: 87154

The best way to achieve this is to specify table as keyspace.table in all queries, and don't rely on the keyspace set in the session. You can of course have multiple session objects - one per keyspace, but it's not recommended, as every session is quite heavyweight, and open at least 2 connections to every node in the cluster.

Recent versions of the Java driver support setting the keyspace via setKeyspace function of the SimpleStatement class, but it requires support on the Cassandra side as well (don't remember which version although, at least DSE 6.0 supports this).

(Update): The setting keyspace on query level isn't merged into OSS Java driver, and available only in DSE Java driver.

Upvotes: 1

Related Questions