I would like to set a Spring_data_cassandra bean that can perform operations on any keyspace that the user can access.
I could use a master username that can write to any keyspace. The keyspace information is dependent on the user that authenticates to Spring, the SpringSecurity Principal user that will authenticate via OAuth2.
I know the username and I know the keyspace that that user needs to write information to. Each user has its own keyspace.
Before I did that with pure Thrift. Create a session with credentials and perform operations on it.
Here is what I did for my previous Rest API using pure Thrift connections other than going through Spring-Data-Cassandra:
tr = new TSocket(host, port);
TFramedTransport tf = new TFramedTransport(tr);
TProtocol proto = new TBinaryProtocol(tf);
client = new Cassandra.Client(proto);
tr.open();
// If a username is provided, set credentials
if (user != null) {
AuthenticationRequest authRequest = new AuthenticationRequest();
Map<String, String> credentials = new HashMap<String, String>();
credentials.put("username", user);
credentials.put("password", password);
authRequest.setCredentials(credentials);
try {
if (client != null)
client.login(authRequest);
} catch (AuthenticationException e2) {
log.error(e2, e2);
throw new Exception("Could not log in: " + e2, e2);
} catch (AuthorizationException e2) {
log.error(e2, e2);
throw new Exception("Could not log in: " + e2, e2);
} catch (TException e2) {
log.error(e2, e2);
throw new Exception("Could not log in: " + e2, e2);
}
}
This creates and authenticates to a Thrift Cassandra.Client
Then, every time I want to perform an operation to Cassandra, I set the Keyspace first.
So, for instance to perform a CQL3 query I do:
CqlResult result = null;
try {
client.set_keyspace(keyspace);
result = client.execute_cql3_query(query, compression, consistency);
} catch (InvalidRequestException e) {
log.error(e.getMessage());
} catch (UnavailableException e) {
log.error(e.getMessage());
} catch (TimedOutException e) {
log.error(e.getMessage());
} catch (SchemaDisagreementException e) {
log.error(e.getMessage());
} catch (TException e) {
log.error(e.getMessage());
}
return result;
These are all org.apache.thrift classes.
Using SpringDataCassandra should make it simpler.
My questions are:
1) Can I create a Cassandra Session without specifying a Keyspace. Can I create a bean and not specify the keyspace?
2) Can I set the keyspace on the bean every time I use it to perform an operation? So I want to set keyspace and perform a CQL.
How can I do that with SpringDataCassandra?
Thanks.