How to use Spring Data Cassandra as I have used Thrift before?

123 views
Skip to first unread message

ScheideckerAntunes

unread,
Jun 17, 2014, 3:23:35 AM6/17/14
to spring-dat...@googlegroups.com
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.

David Webb (Prowave)

unread,
Jun 17, 2014, 8:53:13 AM6/17/14
to ScheideckerAntunes, spring-dat...@googlegroups.com

1) Can I create a Cassandra Session without specifying a Keyspace. Can I create a bean and not specify the keyspace? No

2) Can I set the keyspace on the bean every time I use it to perform an operation? No, its a singleton and you will have concurrency issues.

So I want to set keyspace and perform a CQL. If you use CqlTemplate, with raw CQL, just preface your table names with the keyspace and that should work for you.  You will not be able to do this with CassandraTemplate or the CassandraRepository.

How can I do that with SpringDataCassandra?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Spring Data Cassandra" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spring-data-cass...@googlegroups.com.
To post to this group, send email to spring-dat...@googlegroups.com.
Visit this group at http://groups.google.com/group/spring-data-cassandra.
To view this discussion on the web visit https://groups.google.com/d/msgid/spring-data-cassandra/ea1f0b43-44e9-4894-85e6-76dbea9aa0b9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ScheideckerAntunes

unread,
Jun 17, 2014, 9:47:30 PM6/17/14
to spring-dat...@googlegroups.com, cf.an...@gmail.com, dw...@prowaveconsulting.com
Thanks David. That helps.


On Tuesday, June 17, 2014 6:53:13 AM UTC-6, David Webb (Prowave) wrote:

1) Can I create a Cassandra Session without specifying a Keyspace. Can I create a bean and not specify the keyspace? No

2) Can I set the keyspace on the bean every time I use it to perform an operation? No, its a singleton and you will have concurrency issues.

So I want to set keyspace and perform a CQL. If you use CqlTemplate, with raw CQL, just preface your table names with the keyspace and that should work for you.  You will not be able to do this with CassandraTemplate or the CassandraRepository.

How can I do that with SpringDataCassandra?

Thanks.

--
You received this message because you are subscribed to the Google Groups "Spring Data Cassandra" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spring-data-cassandra+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages